Java LocalDate Of Method

JavaJavaBeginner
Practice Now

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.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL java(("`Java`")) -.-> java/ObjectOrientedandAdvancedConceptsGroup(["`Object-Oriented and Advanced Concepts`"]) java(("`Java`")) -.-> java/BasicSyntaxGroup(["`Basic Syntax`"]) java(("`Java`")) -.-> java/StringManipulationGroup(["`String Manipulation`"]) java(("`Java`")) -.-> java/SystemandDataProcessingGroup(["`System and Data Processing`"]) java/ObjectOrientedandAdvancedConceptsGroup -.-> java/date("`Date`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/packages_api("`Packages / API`") java/BasicSyntaxGroup -.-> java/identifier("`Identifier`") java/BasicSyntaxGroup -.-> java/operators("`Operators`") java/BasicSyntaxGroup -.-> java/output("`Output`") java/StringManipulationGroup -.-> java/strings("`Strings`") java/SystemandDataProcessingGroup -.-> java/system_methods("`System Methods`") subgraph Lab Skills java/date -.-> lab-117830{{"`Java LocalDate Of Method`"}} java/packages_api -.-> lab-117830{{"`Java LocalDate Of Method`"}} java/identifier -.-> lab-117830{{"`Java LocalDate Of Method`"}} java/operators -.-> lab-117830{{"`Java LocalDate Of Method`"}} java/output -.-> lab-117830{{"`Java LocalDate Of Method`"}} java/strings -.-> lab-117830{{"`Java LocalDate Of Method`"}} java/system_methods -.-> lab-117830{{"`Java LocalDate Of Method`"}} end

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.

Other Java Tutorials you may like