Java Month is an enum which represents the 12 months of the year.
Java Month enum methods
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. |
Example
package com.w3schools;
import java.time.LocalDate;
import java.time.Month;
public class TestExample {
public static void main(String args[]){
Month month = Month.from(LocalDate.now());
System.out.println(month.getValue());
System.out.println(month.name());
System.out.println(month.length(true));
//plus() method
System.out.println(month.plus(2));
//minus() method
System.out.println(month.minus(1));
}
} |
package com.w3schools;
import java.time.LocalDate;
import java.time.Month;
public class TestExample {
public static void main(String args[]){
Month month = Month.from(LocalDate.now());
System.out.println(month.getValue());
System.out.println(month.name());
System.out.println(month.length(true));
//plus() method
System.out.println(month.plus(2));
//minus() method
System.out.println(month.minus(1));
}
}
Output