Java Date Time - Month values() example








Month values() returns an array containing the constants of this enum type, in the order they are declared.

Syntax

values has the following syntax.

public static Month[] values()

Example

The following example shows how to use values.

import java.time.Month;

public class Main {
  public static void main(String[] args) {
    for (Month c : Month.values())
       System.out.println(c);

  }
}

The code above generates the following result.





Example 2

Display the number of days in each month of the specified year

import java.time.Month;
import java.time.YearMonth;
//w w w .  jav  a  2s  .c o  m
public class Main {
  public static void main(String[] args) {
    for (Month month : Month.values()) {
      YearMonth ym = YearMonth.of(2014, month);
      System.out.printf("%s: %d days%n", month, ym.lengthOfMonth());
    }
  }
}

The code above generates the following result.