LAST UPDATED: NOVEMBER 24, 2020
Java LocalDate of() Method
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 passing year, month and day as parameters. It returns a local date after creating a date from the parameters. For example, we have three integer values : 2012, 10 and 20 then we can get a date by using these values and the method will returns a date like : 2012-10-20.
It returns a LocalDate
with the specified year, month and day-of-month. The day 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, int month, int dayOfMonth)
Parameters:
year - It is an integer value that represents the a valid year.
month - It is an integer value that represents the a valid month.
dayOfMonth - It is an integer value that represents the 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 passing year, month and day as parameters. Here, we can see that we get a valid year as a result.
import java.time.LocalDate;
public class DateDemo {
public static void main(String[] args){
LocalDate localDate = LocalDate.of(2012,10,12);
System.out.println(localDate);
}
}
2012-10-12
Live Example:
Try with a live example, execute the code instantly with our powerful Online Java Compiler.