LAST UPDATED: NOVEMBER 24, 2020
Java LocalDate of() Method with Month Enum
Java LocalDate of()
method is used to obtain an instance of LocalDate
from a year, month, and day. It can be used to create a date by a passing year, month, and day as parameters. It returns a local date after creating a date from the parameters.
For the value of the month, it uses Month
enum that provides all the months of a year.
It returns a LocalDate
with the specified year, month and day-of-month. The date must be valid for the year and month, otherwise, an exception will be thrown. The syntax of the method is given below.
Syntax
public static LocalDate of(int year, Month month, int dayOfMonth)
Parameters:
year - It is an integer value that represents a valid year.
month - It is a Month
enum value that represents a valid month.
dayOfMonth - It is an integer value that represents a valid day.
Returns:
It returns a local date after combining the specified year, month and day.
Time for an Example:
Let's take an example to create a date by a passing year, month enum, and day as parameters. Here, we can see that we get a valid year as a result.
import java.time.LocalDate;
import java.time.Month;
public class DateDemo {
public static void main(String[] args){
LocalDate localDate = LocalDate.of(2020,Month.APRIL,10);
System.out.println(localDate);
}
}
2020-04-10
Live Example:
Try with a live example, execute the code instantly with our powerful Online Java Compiler.