Java Data Type How to - Get month name and value








Question

We would like to know how to get month name and value.

Answer

import java.time.LocalDate;
import java.time.Month;
//from w w w. j  a va 2  s  .c  o  m
public class Main {

  public static void main(String[] args) {
    LocalDate date = LocalDate.of(2014, 2, 15); // 2014-02-15
    Month february = date.getMonth();
    System.out.println(february); // FEBRUARY

    int februaryIntValue = february.getValue();
    System.out.println(februaryIntValue); // 2

    // 28 , 29
    System.out.println(String.format("%s , %s", february.minLength(), february.maxLength()));

    Month firstMonthOfQuarter = february.firstMonthOfQuarter();
    System.out.println(firstMonthOfQuarter); // JANUARY
  }
}

The code above generates the following result.