Month

Description

Month enum has 12 constants to represents the 12 months.

The constant names are JANUARY, FEBRUARY, MARCH, APRIL, MAY, JUNE, JULY, AUGUST, SEPTEMBER, OCTOBER, NOVEMBER, and DECEMBER.

Month enum are numbered sequentially from 1 to 12, where January is 1 and December is 12.

Example

Month enum of() creates an instance of Month from an int value.

We can use from() to create the Month from date object.

To get the int value of the Month use Month enum getValue() method.


import java.time.LocalDate;
import java.time.Month;
//ww w . j a  v a2 s .c  o m
public class Main {
  public static void main(String[] args) {
    LocalDate localDate  = LocalDate.of(2014, Month.AUGUST, 3);
    System.out.println(localDate);
    Month month1  = Month.from(localDate);
    System.out.println(month1);
    
    Month month2  = Month.of(2);
    System.out.println(month2);
    
    Month month3  = month2.plus(2);
    System.out.println(month3);
    
    Month month4  = localDate.getMonth();
    System.out.println(month4);
    
    int monthIntValue  = month2.getValue();
    System.out.println(monthIntValue);
  }
}

The code above generates the following result.





















Home »
  Java Date Time »
    Tutorial »




Java Date Time Tutorial