YearMonth format() method in Java Last Updated : 04 Sep, 2021 Comments Improve Suggest changes Like Article Like Report The format() method of YearMonth class in Java is used to format this YearMonth instance according to a specified DateTimeFormatter for year-month passed as parameter to this method. Syntax: public String format(DateTimeFormatter formatter) Parameter: This method accepts a single parameter formatter which is the DateTimeFormatter according to which this YearMonth instance will be formatted.Return Value: It returns the value of this YearMonth as a string after formatting it according to the specified formatter.Below programs illustrate the format() method of YearMonth in Java: Program 1: Java // Program to illustrate the format() method import java.util.*; import java.time.*; import java.time.format.DateTimeFormatter; public class GfG { public static void main(String[] args) { // Create a YearMonth object YearMonth thisYearMonth = YearMonth.of(2017, 8); // Create a DateTimeFormatter string DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yy/MM"); // Format this year-month System.out.println(thisYearMonth.format(formatter)); } } Output: 17/08 Program 2: Java // Program to illustrate the format() method import java.util.*; import java.time.*; import java.time.format.DateTimeFormatter; public class GfG { public static void main(String[] args) { // Create a YearMonth object YearMonth thisYearMonth = YearMonth.of(2018, 5); // Create a DateTimeFormatter string DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM/yy"); // Format this year-month System.out.println(thisYearMonth.format(formatter)); } } Output: 05/18 Reference: https://docs.oracle.com/javase/8/docs/api/java/time/YearMonth.html#format-java.time.format.DateTimeFormatter- Comment More infoAdvertise with us Next Article YearMonth format() method in Java gopaldave Follow Improve Article Tags : Misc Java Java-Functions Java-time package Java-YearMonth +1 More Practice Tags : JavaMisc Similar Reads YearMonth getMonth() method in Java The getMonth() method of YearMonth class in Java is used to get the month field from this YearMonth instance with which it is used. It returns the month field as a Month instance. Syntax: public Month getMonth() Parameter: This method does not accepts any parameter. Return Value: It returns the mont 1 min read YearMonth getYear() method in Java The getYear() method of YearMonth class in Java is used to get the value of the Year field from this YearMonth instance with which it is used. It returns the value of the year field as an integer from MIN_YEAR to MAX_YEAR. Syntax: public int getYear() Parameter: This method does not accepts any para 1 min read YearMonth atEndOfMonth() method in Java The atEndOfMonth() method of YearMonth class in Java is used to return a LocalDate of the last day of month based on this YearMonth object with which it is used. Syntax: public LocalDate atEndOfMonth() Parameter: This method does not accepts any parameter. Return Value: It returns a Local Date with 2 min read YearMonth hashCode() method in Java The hashCode() method of YearMonth class in Java is used to create a suitable hash code for this YearMonth instance with which it is used. Syntax: public int hashCode() Parameter: This method does not accepts any parameter. Return Value: It returns a suitable integral hash code value for this YearMo 1 min read Year atDay() method in Java The atDay() method of Year class in Java combines the current year with a day-of-year passed as parameter to it to create a LocalDate. Syntax: public LocalDate atDay(int dayOfYear) Parameter: This method accepts a single parameter dayOfYear. It is the day-of-year to use. It can take values from 1 to 2 min read YearMonth getMonthValue() method in Java The getMonthValue() method of YearMonth class in Java is used to get the value of the month field from this YearMonth instance with which it is used. It returns the value of the month field as an integer between 1-12 denoting months from January to December. Syntax: public int getMonthValue() Parame 1 min read YearMonth get() method in Java with Examples The get() method of the YearMonth class in Java is used to get the value of the specified field from this year-month as an integer value. This method queries this year-month for the value of the specified field. The returned value will always be within the valid range. An exception is thrown if it i 2 min read Year format() method in Java with Examples The format() method of Year class in Java is used to format the current Year object according to the DateTimeFormatter passed to it as a parameter. Syntax: public String format(DateTimeFormatter formatter) Parameter: This method accepts a single parameter formatter. It specifies a DateTimeFormatter 2 min read Year atMonthDay() method in Java The atMonthDay(MonthDay) method of Year class in Java combines the current year object with a month-day object passed as parameter to it to create a LocalDate object. Syntax: public LocalDate atMonthDay(MonthDay monthDay) Parameter: This method accepts a single parameter monthDay. It is the MonthDay 2 min read MonthDay format() method in Java with Examples The format() method of MonthDay class in Java formats this month-day using the specified formatter. Syntax: public String format(DateTimeFormatter formatter) Parameter: This method accepts a parameter formatter which specifies the formatter to use and it is not null. Returns: The function returns th 1 min read Like