Introduction
In this lab, we will learn about the Java LocalDate of() method which is used to obtain an instance of LocalDate from a given year, month, and day-of-month. This method returns a LocalDate object that represents the date created from the specified parameters.
Import the required class
To use the LocalDate class, we need to import it. Add the following import statement at the beginning of the file.
import java.time.LocalDate;
Use the of() method
Create a date by passing year, month, and day as parameters using the of() method. It returns a LocalDate object after creating a date from the parameters. In the following example, we create a local date for October 12, 2012.
LocalDate localDate = LocalDate.of(2012,10,12);
Print the created date
Print the created date using the System.out.println() method as shown below.
System.out.println("Local date created using of() method: " + localDate);
Compile and run the program
To compile the program, run the following command in the terminal.
javac LocalDateOfDemo.java
To run the program, execute the following command.
java LocalDateOfDemo
Verify the output
Verify that the program output shows the correct local date created using the of() method.
Local date created using of() method: 2012-10-12
Summary
In this lab, we learned how to use the Java LocalDate of() method to create a date by specifying the year, month, and day-of-month as parameters. We created a local date for October 12, 2012, and verified the output. The of() method is useful when we want to create a LocalDate object for a specified date.



