Java LocalDate From Method

JavaJavaBeginner
Practice Now

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.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL java(("`Java`")) -.-> java/ProgrammingTechniquesGroup(["`Programming Techniques`"]) java(("`Java`")) -.-> java/ObjectOrientedandAdvancedConceptsGroup(["`Object-Oriented and Advanced Concepts`"]) java(("`Java`")) -.-> java/BasicSyntaxGroup(["`Basic Syntax`"]) java(("`Java`")) -.-> java/DataStructuresGroup(["`Data Structures`"]) java(("`Java`")) -.-> java/SystemandDataProcessingGroup(["`System and Data Processing`"]) java/ProgrammingTechniquesGroup -.-> java/scope("`Scope`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/classes_objects("`Classes/Objects`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/class_methods("`Class Methods`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/date("`Date`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/modifiers("`Modifiers`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/oop("`OOP`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/packages_api("`Packages / API`") java/BasicSyntaxGroup -.-> java/identifier("`Identifier`") java/DataStructuresGroup -.-> java/arrays("`Arrays`") java/BasicSyntaxGroup -.-> java/comments("`Comments`") java/BasicSyntaxGroup -.-> java/data_types("`Data Types`") java/BasicSyntaxGroup -.-> java/operators("`Operators`") java/BasicSyntaxGroup -.-> java/output("`Output`") java/SystemandDataProcessingGroup -.-> java/system_methods("`System Methods`") subgraph Lab Skills java/scope -.-> lab-117780{{"`Java LocalDate From Method`"}} java/classes_objects -.-> lab-117780{{"`Java LocalDate From Method`"}} java/class_methods -.-> lab-117780{{"`Java LocalDate From Method`"}} java/date -.-> lab-117780{{"`Java LocalDate From Method`"}} java/modifiers -.-> lab-117780{{"`Java LocalDate From Method`"}} java/oop -.-> lab-117780{{"`Java LocalDate From Method`"}} java/packages_api -.-> lab-117780{{"`Java LocalDate From Method`"}} java/identifier -.-> lab-117780{{"`Java LocalDate From Method`"}} java/arrays -.-> lab-117780{{"`Java LocalDate From Method`"}} java/comments -.-> lab-117780{{"`Java LocalDate From Method`"}} java/data_types -.-> lab-117780{{"`Java LocalDate From Method`"}} java/operators -.-> lab-117780{{"`Java LocalDate From Method`"}} java/output -.-> lab-117780{{"`Java LocalDate From Method`"}} java/system_methods -.-> lab-117780{{"`Java LocalDate From Method`"}} end

Set up the Java Development Environment

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) {

    }
}

Use LocalDate From A Custom Date

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

Use LocalDate From The Current Date

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.

Use LocalDate From A ZonedDateTime Object

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.

Summary

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.

Other Java Tutorials you may like