Javatpoint Logo
Javatpoint Logo

Java YearMonth class

Java YearMonth class is an immutable date-time object that represents the combination of a year and month. It inherits the Object class and implements the Comparable interface.

Java YearMonth class declaration

Let's see the declaration of java.time.YearMonth class.

Methods of Java YearMonth

Method Description
Temporal adjustInto(Temporal temporal) It is used to adjust the specified temporal object to have this year-month.
String format(DateTimeFormatter formatter) It is used to format this year-month using the specified formatter.
int get(TemporalField field) It is used to get the value of the specified field from this year-month as an int.
boolean isLeapYear() It is used to check if the year is a leap year, according to the ISO proleptic calendar system rules.
static YearMonth now() It is used to obtain the current year-month from the system clock in the default time zone.
static YearMonth of(int year, int month) It is used to obtain an instance of YearMonth from a year and month.
YearMonth plus(TemporalAmount amountToAdd) It is used to return a copy of this year-month with the specified amount added.
YearMonth minus (TemporalAmount amountToSubtract) It is used to return a copy of this year-month with the specified amount subtracted.
LocalDate atEndOfMonth() It returns a LocalDate at the end of the month.
int compareTo(YearMonth other) It compares this year-month to another year-month.
boolean equals(Object obj) It checks if this year-month is equal to another year-month.
static YearMonth now(Clock clock) It obtains the current year-month from the specified clock.
static YearMonth of(int year, int month) It obtains an instance of YearMonth from a year and month.
long until(Temporal endExclusive, TemporalUnit unit) It calculates the amount of time until another year-month in terms of the specified unit.
YearMonth withMonth(int month) It returns a copy of this YearMonth with the month-of-year altered.
YearMonth withYear(int year) It returns a copy of this YearMonth with the year altered.

Java YearMonth Example: now()

YearMonthExample1.java

Test it Now

Output:

2017-01  

Java YearMonth Example: format()

YearMonthExample2.java

Test it Now

Output:

01 2017

Java YearMonth Example: get()

YearMonthExample3.java

Test it Now

Output:

2017
1

Java YearMonth Example: plus()

YearMonthExample4.java

Test it Now

Output:

2019-01

Java YearMonth Example: minus()

YearMonthExample5.java

Test it Now

Output:

2015-01

Next TopicJava Period





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA