Java YearMonth

Education is not limited to just classrooms. It can be gained anytime, anywhere... - Ravi Ranjan (M.Tech-NIT)

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.

  1. public final class YearMonth extends Object   

  2. implements Temporal, TemporalAdjuster, Comparable, Serializable  

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.

Java YearMonth Example: now()

  1. import java.time.YearMonth;  
  2. public class YearExample {  
  3. public static void main(String[] args) {  
  4.   YearMonth ym = YearMonth.now();  
  5.   System.out.println(ym);  
  6. }  
  7. }  

Output:

2017-01  

Java YearMonth Example: format()

  1. import java.time.YearMonth;  
  2. import java.time.format.DateTimeFormatter;  
  3. public class YearExample {  
  4.   public static void main(String[] args) {  
  5.     YearMonth ym = YearMonth.now();  
  6.     String s = ym.format(DateTimeFormatter.ofPattern("MM yyyy"));  
  7.     System.out.println(s);  
  8.   }  
  9. }  

Output:

01 2017

Java YearMonth Example: get()

  1. import java.time.YearMonth;  
  2. import java.time.temporal.ChronoField;  
  3. public class YearExample {  
  4.   public static void main(String[] args) {  
  5.     YearMonth y = YearMonth.now();  
  6.     long l1 = y.get(ChronoField.YEAR);  
  7.     System.out.println(l1);  
  8.     long l2 = y.get(ChronoField.MONTH_OF_YEAR);  
  9.     System.out.println(l2);  
  10.   }  
  11. }  

Output:

2017
1

Java YearMonth Example: plus()

  1. import java.time.*;  
  2. public class YearExample {  
  3.   public static void main(String[] args) {  
  4.     YearMonth ym1 = YearMonth.now();  
  5.     YearMonth ym2 = ym1.plus(Period.ofYears(2));  
  6.     System.out.println(ym2);  
  7.   }  
  8. }  

Output:

2019-01

Java YearMonth Example: minus()

  1. import java.time.*;  
  2. public class YearExample {  
  3.   public static void main(String[] args) {  
  4.     YearMonth ym1 = YearMonth.now();  
  5.     YearMonth ym2 = ym1.minus(Period.ofYears(2));  
  6.     System.out.println(ym2);  
  7.   }  
  8. }  

Output:

2015-01