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.

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