Java LocalDate getEra Method

JavaJavaBeginner
Practice Now

Introduction

The getEra() method in the LocalDate class is used to get the era of a date. The IsoChronology class defines eras CE (Current Era) from year one onwards and BCE (Before Current Era) from year zero backward. This method does not take any arguments and returns an IsoEra enum value.


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/StringManipulationGroup(["`String Manipulation`"]) 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/StringManipulationGroup -.-> java/strings("`Strings`") java/SystemandDataProcessingGroup -.-> java/system_methods("`System Methods`") subgraph Lab Skills java/scope -.-> lab-117792{{"`Java LocalDate getEra Method`"}} java/classes_objects -.-> lab-117792{{"`Java LocalDate getEra Method`"}} java/class_methods -.-> lab-117792{{"`Java LocalDate getEra Method`"}} java/date -.-> lab-117792{{"`Java LocalDate getEra Method`"}} java/modifiers -.-> lab-117792{{"`Java LocalDate getEra Method`"}} java/oop -.-> lab-117792{{"`Java LocalDate getEra Method`"}} java/packages_api -.-> lab-117792{{"`Java LocalDate getEra Method`"}} java/identifier -.-> lab-117792{{"`Java LocalDate getEra Method`"}} java/arrays -.-> lab-117792{{"`Java LocalDate getEra Method`"}} java/comments -.-> lab-117792{{"`Java LocalDate getEra Method`"}} java/data_types -.-> lab-117792{{"`Java LocalDate getEra Method`"}} java/operators -.-> lab-117792{{"`Java LocalDate getEra Method`"}} java/output -.-> lab-117792{{"`Java LocalDate getEra Method`"}} java/strings -.-> lab-117792{{"`Java LocalDate getEra Method`"}} java/system_methods -.-> lab-117792{{"`Java LocalDate getEra Method`"}} end

Create a new Java class in the project directory

We will create a new Java class in the project directory named DateEraExample.java. First, navigate to the project directory.

cd ~/project

Now create a new file named DateEraExample.java using the nano editor.

touch DateEraExample.java

Implement the Java code to demonstrate the getEra() method

In the DateEraExample.java file, we will create a Java program to demonstrate the getEra() method in the LocalDate class.

import java.time.LocalDate;
import java.time.chrono.IsoEra;

public class DateEraExample {
    public static void main(String[] args) {

        // Creating a date with year 2021
        LocalDate localDate = LocalDate.of(2021, 11, 10);

        // Printing the date
        System.out.println("Date: " + localDate);

        // Getting the era of the specified date
        IsoEra era = localDate.getEra();

        // Printing the era
        System.out.println("Era: " + era);

        // Creating a date with year 0
        LocalDate zeroDate = LocalDate.of(0, 1, 1);

        // Printing the date
        System.out.println("Date with year zero: " + zeroDate);

        // Getting the era of the specified date
        era = zeroDate.getEra();

        // Printing the era
        System.out.println("Era: " + era);
    }
}

Compile and run the Java program

Save the changes to the DateEraExample.java file and exit the editor. Now compile the Java program using the following command in the terminal.

javac DateEraExample.java

After compiling the program, execute the program using the following command.

java DateEraExample

The output of the program should be displayed on the terminal.

Date: 2021-11-10
Era: CE
Date with year zero: 0000-01-01
Era: BCE

Summary

In this lab, we learned how to use the getEra() method in the LocalDate class of Java to get the era of a date. The IsoChronology class defines eras CE (Current Era) from year one onwards and BCE (Before Current Era) from year zero backward. This method returns an IsoEra enum value representing the era of the date.

Other Java Tutorials you may like