Sunday, October 19, 2025
HomeLanguagesJavaMonth of() method in Java

Month of() method in Java

The of() method is a built-in method of the Month ENUM which is used to generate a Month instance from an integer value. The integer value should be in the range 1-12 representing any of the 12 months and the method generates a Month instance from it representing a month-of-year.

Syntax:

public static Month of(int month)

Parameters: This method accepts a single parameter month, which is of integer type.

Return Value: This method returns the corresponding Month instance generated using the parameter passed.

Exception: This method throws a DateTimeException if the month of year passed to the parameter is not valid.

Below programs illustrate the above method:

Program 1:




import java.time.*;
import java.time.Month;
import java.time.temporal.ChronoField;
  
class monthEnum {
    public static void main(String[] args)
    {
        // Create a month instance
        Month month = Month.of(2);
  
        // Print the month Instance
        System.out.println(month);
    }
}


Output:

FEBRUARY

Program 2:




import java.time.*;
import java.time.Month;
import java.time.temporal.ChronoField;
  
class monthEnum {
    public static void main(String[] args)
    {
        // Create a month instance
        Month month = Month.of(12);
  
        // Print the month Instance
        System.out.println(month);
    }
}


Output:

DECEMBER

Reference: https://docs.oracle.com/javase/8/docs/api/java/time/Month.html#of-int-

Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32361 POSTS0 COMMENTS
Milvus
88 POSTS0 COMMENTS
Nango Kala
6728 POSTS0 COMMENTS
Nicole Veronica
11892 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11954 POSTS0 COMMENTS
Shaida Kate Naidoo
6852 POSTS0 COMMENTS
Ted Musemwa
7113 POSTS0 COMMENTS
Thapelo Manthata
6805 POSTS0 COMMENTS
Umr Jansen
6801 POSTS0 COMMENTS