Introduction
The LocalDate from(TemporalAccessor temporal)
method is used to get a localdate object from a TemporalAccessor instance. It accepts a temporal object and converts it into a localdate object.
The LocalDate from(TemporalAccessor temporal)
method is used to get a localdate object from a TemporalAccessor instance. It accepts a temporal object and converts it into a localdate object.
Firstly, set up the Java development environment on your computer. You can use any text editor or integrated development environment (IDE) of your choice.
Create a file named LocalDateFromMethod.java
in the ~/project
directory.
Then, add the following code to the file:
import java.time.LocalDate;
import java.time.temporal.TemporalAccessor;
public class LocalDateFromMethod {
public static void main(String[] args) {
}
}
In this step, you will create a localdate object using the from()
method and a temporalaccessor instance. The temporalaccessor instance contains a custom date.
Add the following code inside the main()
method to create a localdate object from a custom date:
// create a temporalaccessor object with a custom date
TemporalAccessor date = LocalDate.of(2022, 9, 1);
// get the localdate object from the temporalaccessor object
LocalDate localDate = LocalDate.from(date);
// print the localdate
System.out.println(localDate);
Save the file and open the terminal. Compile and run the code with the following command:
javac LocalDateFromMethod.java && java LocalDateFromMethod
You should see the following output:
2022-09-01
In this step, you will create a localdate object using the from()
method and a temporalaccessor instance. The temporalaccessor instance contains the current date and time.
Add the following code inside the main()
method to create a localdate object from the current date:
// create a temporalaccessor object with the current date and time
TemporalAccessor date = LocalDate.now();
// get the localdate object from the temporalaccessor object
LocalDate localDate = LocalDate.from(date);
// print the localdate
System.out.println(localDate);
Save the file and open the terminal. Compile and run the code with the following command:
javac LocalDateFromMethod.java && java LocalDateFromMethod
You should see the current date in the output.
In this step, you will create a localdate object using the from()
method and a temporalaccessor instance. The temporalaccessor instance contains a ZonedDateTime object.
Add the following code inside the main()
method to create a temporalaccessor instance with a ZonedDateTime object and then convert it into a localdate object using the from()
method:
// create a ZonedDateTime object to represent the current date and time
ZonedDateTime zonedDateTime = ZonedDateTime.now();
// create a temporalaccessor object with the ZonedDateTime object
TemporalAccessor date = zonedDateTime;
// get the localdate object from the temporalaccessor object
LocalDate localDate = LocalDate.from(date);
// print the localdate
System.out.println(localDate);
Save the file and open the terminal. Compile and run the code with the following command:
javac LocalDateFromMethod.java && java LocalDateFromMethod
You should see the current date in the output.
In this lab, you learned how to use the LocalDate from()
method to create a localdate object from a temporalaccessor instance. You learned how to create a temporalaccessor instance with a custom date, the current date and time, and a ZonedDateTime object. You also learned how to print the localdate object in the output.