Java Month enum

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

Java Month enum

In Java the Month is an enum representing the 12 months of the year. In addition with the textual enum name, every month-of-year has an int value.


Java Month enum declaration

Let's see the declaration of java.time.Month.

  1. public enum Month extends Enum implements TemporalAccessor, TemporalAdjuster  

Methods of Java Month

Method

Description

int getValue()

It is used to get the month-of-year int value

int get(TemporalField field)

It is used to get the value of the specified field from this month-of-year as an int.

int length(boolean leapYear)

It is used to get the length of this month in days.

int maxLength()

It is used to get the maximum length of this month in days.

int minLength()

It is used to get the minimum length of this month in days.

Month minus(long months)

It is used to return the month-of-year that is the specified number of months before this one.

Month plus(long months)

It is used to return the month-of-year that is the specified number of quarters after this one.

static Month of(int month)

It is used to obtain an instance of Month from an int value.

Java Month enum Example

  1. import java.time.*;  
  2. import java.time.temporal.*;  
  3. public class MonthExample {  
  4.     public static void main(String[] args) {  
  5.         Month month = Month.valueOf("January".toUpperCase());  
  6.         System.out.printf("For the month of %s all Sunday are:%n", month);  
  7.         LocalDate localdate = Year.now().atMonth(month).atDay(1).  
  8.         with(TemporalAdjusters.firstInMonth(DayOfWeek.SUNDAY));  
  9.         Month mi = localdate.getMonth();  
  10.         while (mi == month) {  
  11.             System.out.printf("%s%n", localdate);  
  12.             localdate = localdate.with(TemporalAdjusters.next(DayOfWeek.SUNDAY));  
  13.             mi = localdate.getMonth();  
  14.         }  
  15.     }  
  16. }  

Output:

For the month of JANUARY all Sunday are:
2017-01-01
2017-01-08
2017-01-15
2017-01-22
2017-01-29

Java Month enum Example: getValue()

  1. import java.time.*;  
  2. public class MonthExample {  
  3.   public static void main(String[] args) {  
  4.     Month month = Month.from(LocalDate.now());  
  5.     System.out.println(month.getValue());  
  6.     System.out.println(month.name());  
  7.   }  
  8. }  

Output:

1
JANUARY

Java Month enum Example: minus()

  1. import java.time.*;  
  2. public class MonthExample {  
  3.   public static void main(String[] args) {  
  4.     Month month = Month.from(LocalDate.now());    
  5.     System.out.println(month.minus(2));  
  6.   }  
  7. }  

Output:

NOVEMBER

Java Month enum Example: plus()

  1. import java.time.*;  
  2. public class MonthExample {  
  3.   public static void main(String[] args) {  
  4.     Month month = Month.from(LocalDate.now());    
  5.     System.out.println(month.plus(2));  
  6.   }  
  7. }  

Output:

MARCH

Java Month enum Example: length()

  1. import java.time.*;  
  2. public class MonthExample {  
  3.   public static void main(String[] args) {  
  4.     Month month = Month.from(LocalDate.now());    
  5.     System.out.println("Total Number of days: "+month.length(true));  
  6.   }  
  7. }  

Output:

Total Number of days: 31