📘 Premium Read: Access my best content on Medium member-only articles — deep dives into Java, Spring Boot, Microservices, backend architecture, interview preparation, career advice, and industry-standard best practices.
✅ Some premium posts are free to read — no account needed. Follow me on Medium to stay updated and support my writing.
🎓 Top 10 Udemy Courses (Huge Discount): Explore My Udemy Courses — Learn through real-time, project-based development.
▶️ Subscribe to My YouTube Channel (172K+ subscribers): Java Guides on YouTube
Java Month Enum Class Diagram
Java Month Enum Methods/APIs with Examples
Java Month Enum Methods/APIs
- int firstDayOfYear(boolean leapYear) - Gets the day-of-year corresponding to the first day of this month.
- Month firstMonthOfQuarter() - Gets the month corresponding to the first month of this quarter.
- static Month from(TemporalAccessor temporal) - Obtains an instance of Month from a temporal object.
- int get(TemporalField field) - Gets the value of the specified field from this month-of-year as an int.
- String getDisplayName(TextStyle style, Locale locale) - Gets the textual representation, such as 'Jan' or 'December'.
- long getLong(TemporalField field) - Gets the value of the specified field from this month-of-year as a long.
- int getValue() - Gets the month-of-year int value.
- boolean isSupported(TemporalField field) - Checks if the specified field is supported.
- int length(boolean leapYear) - Gets the length of this month in days.
- int maxLength() - Gets the maximum length of this month in days.
- int minLength() - Gets the minimum length of this month in days.
- Month minus(long months) - Returns the month-of-year that is the specified number of months before this one.
- static Month of(int month) - Obtains an instance of Month from an int value.
- Month plus(long months) - Returns the month-of-year that is the specified number of quarters after this one.
- ValueRange range(TemporalField field) - Gets the range of valid values for the specified field.
- static Month valueOf(String name) - Returns the enum constant of this type with the specified name.
- static Month[] values() - Returns an array containing the constants of this enum type, in the order they are declared.
Month Enum Methods/APIs Examples
package net.javaguides.date;
import java.time.Month;
import java.time.ZonedDateTime;
import java.time.format.TextStyle;
import java.time.temporal.ChronoField;
import java.time.temporal.TemporalQueries;
import java.util.Locale;
public class MonthMethodsExample {
public static void main(String[] args) {
MonthMethodsExample example = new MonthMethodsExample();
example.valuesMethod();
example.valueOfMethodExample();
example.rangeMethodExample();
example.queryMethodExample();
example.plusMethodExample();
example.ofMethodExample();
example.minusMethodExample();
example.minLengthMethodExample();
example.maxLengthMethodExample();
example.getValueMethodExample();
example.getLongMethodExample();
example.getDisplayNameMethodExample();
example.getMethodExample();
example.fromMethodExample();
example.firstMonthOfQuarterMethodExample();
example.firstDayOfYearMethodExample();
}
public void valuesMethod() {
for (Month month: Month.values()) {
System.out.println(month);
}
}
public void valueOfMethodExample() {
Month month = Month.valueOf("MARCH");
System.out.println(month);
}
public void rangeMethodExample() {
Month day = Month.of(2);
System.out.println("Range : " + day.range(ChronoField.MONTH_OF_YEAR));
}
public void queryMethodExample() {
Month month = Month.of(2);
System.out.printf("Month precision is %s%n", month.query(TemporalQueries.precision()));
}
public void plusMethodExample() {
Month month = Month.of(5);
System.out.println(month);
System.out.println(month.plus(1));
}
public void ofMethodExample() {
Month month = Month.of(3);
System.out.println(month);
}
public void minusMethodExample() {
Month month = Month.of(5);
System.out.println(month);
System.out.println(month.minus(1));
}
public void minLengthMethodExample() {
Month month = Month.FEBRUARY;
System.out.println(month.minLength());
}
public void maxLengthMethodExample() {
Month month = Month.FEBRUARY;
System.out.println(month.maxLength());
}
public void getValueMethodExample() {
Month month = Month.FEBRUARY;
System.out.println(month.getValue());
}
public void getLongMethodExample() {
Month day = Month.FEBRUARY;
System.out.println(day.getLong(ChronoField.MONTH_OF_YEAR));
}
public void getDisplayNameMethodExample() {
Month day = Month.of(3);
System.out.println(day.getDisplayName(TextStyle.SHORT, Locale.ENGLISH));
}
public void getMethodExample() {
Month day = Month.FEBRUARY;
System.out.println(day.get(ChronoField.MONTH_OF_YEAR));
}
public void fromMethodExample() {
Month day = Month.from(ZonedDateTime.now());
System.out.println(day);
}
public void firstMonthOfQuarterMethodExample() {
Month month = Month.of(3);
System.out.println(month.firstMonthOfQuarter());
}
public void firstDayOfYearMethodExample() {
Month month = Month.of(3);
System.out.println(month.firstDayOfYear(false));
}
}
JANUARY
FEBRUARY
MARCH
APRIL
MAY
JUNE
JULY
AUGUST
SEPTEMBER
OCTOBER
NOVEMBER
DECEMBER
MARCH
Range : 1 - 12
Month precision is Months
MAY
JUNE
MARCH
MAY
APRIL
28
29
2
2
Mar
2
DECEMBER
JANUARY
60
Comments
Post a Comment
Leave Comment