Java LocalDate GetChronology Method

JavaJavaBeginner
Practice Now

Introduction

The getChronology() method is a part of the LocalDate class in Java. This method returns the chronology of a date, which refers to the calendar system used to represent that date. In this lab, we will learn how to use getChronology() method with step-by-step instructions.


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/data_types("`Data Types`") java/BasicSyntaxGroup -.-> java/operators("`Operators`") java/BasicSyntaxGroup -.-> java/output("`Output`") java/StringManipulationGroup -.-> java/strings("`Strings`") java/BasicSyntaxGroup -.-> java/variables("`Variables`") java/SystemandDataProcessingGroup -.-> java/string_methods("`String Methods`") java/SystemandDataProcessingGroup -.-> java/system_methods("`System Methods`") subgraph Lab Skills java/date -.-> lab-117784{{"`Java LocalDate GetChronology Method`"}} java/packages_api -.-> lab-117784{{"`Java LocalDate GetChronology Method`"}} java/identifier -.-> lab-117784{{"`Java LocalDate GetChronology Method`"}} java/data_types -.-> lab-117784{{"`Java LocalDate GetChronology Method`"}} java/operators -.-> lab-117784{{"`Java LocalDate GetChronology Method`"}} java/output -.-> lab-117784{{"`Java LocalDate GetChronology Method`"}} java/strings -.-> lab-117784{{"`Java LocalDate GetChronology Method`"}} java/variables -.-> lab-117784{{"`Java LocalDate GetChronology Method`"}} java/string_methods -.-> lab-117784{{"`Java LocalDate GetChronology Method`"}} java/system_methods -.-> lab-117784{{"`Java LocalDate GetChronology Method`"}} end

Create a Java file

First, let's create a Java file called DateDemo.java by running the following command in the terminal:

touch DateDemo.java

Import classes

We need to import the LocalDate and IsoChronology classes to use them in our example. Add the following code at the beginning of the DateDemo.java file:

import java.time.LocalDate;
import java.time.chrono.IsoChronology;

Create a LocalDate object

Letโ€™s create a LocalDate object representing a specific date. Add the following code in the main method:

LocalDate localDate = LocalDate.of(2018, 2, 20);

This will create a LocalDate object corresponding to February 20th, 2018.

Get chronology using getChronology() method

Now, we will fetch the chronology of the date using getChronology() method. Add the following code:

IsoChronology iso = localDate.getChronology();

This will create an IsoChronology object representing the ISO-8601 calendar system.

Print the chronology

Finally, we will print the chronology of the date using the println() method. Add the following code:

System.out.println("Date chronology: " + iso);

This will print the chronology of the date in the console.

Compile and run the Java program

Compile the DateDemo.java file using the following command:

javac DateDemo.java

Run the program with the following command:

java DateDemo

Create a LocalDate object from a string

We can also create a LocalDate object from a string that represents a date. Add the following code in the main method:

String date = "2015-02-06";
LocalDate localDate = LocalDate.parse(date);

This will create a LocalDate object representing February 6th, 2015.

Get chronology using getChronology() method

Now, letโ€™s get the chronology of the date using getChronology() method. Add the following code:

IsoChronology iso = localDate.getChronology();

This will create an IsoChronology object representing the ISO-8601 calendar system.

Print the chronology

Finally, we will print the chronology of the date using the println() method. Add the following code:

System.out.println("Date chronology: " + iso);

This will print the chronology of the date in the console.

Compile and run the Java program

Compile the DateDemo.java file using the following command:

javac DateDemo.java

Run the program with the following command:

java DateDemo

Summary

In this lab, we learned how to use the getChronology() method of the LocalDate class in Java. We learned that the getChronology() method is used to fetch the chronology of a date and returns an IsoChronology object representing the calendar system used to represent that date. We also learned how to create a LocalDate object from a string and print the chronology of the date in the console using getChronology() method.

Other Java Tutorials you may like