ZetCode

Java AbstractChronology Class

Last modified: April 16, 2025

The java.time.chrono.AbstractChronology is an abstract base class for calendar systems. It provides the framework for different chronology implementations like ISO, Hijrah, or Japanese calendars.

AbstractChronology implements the Chronology interface. It serves as the foundation for calendar-specific operations. The class handles date creation, field management, and temporal queries across different calendars.

AbstractChronology Class Overview

AbstractChronology provides common functionality for all chronology implementations. Key operations include date creation, era handling, and calendar system identification. The class is part of Java's comprehensive date-time API.

public abstract class AbstractChronology implements Chronology {
    public abstract String getId();
    public abstract String getCalendarType();
    public ChronoLocalDate date(int prolepticYear, int month, int dayOfMonth);
    public ChronoLocalDate date(Era era, int yearOfEra, int month, int dayOfMonth);
    public ChronoLocalDate dateYearDay(int prolepticYear, int dayOfYear);
    public ChronoLocalDate dateYearDay(Era era, int yearOfEra, int dayOfYear);
    public ChronoLocalDate date(TemporalAccessor temporal);
    public boolean isLeapYear(long prolepticYear);
    public int prolepticYear(Era era, int yearOfEra);
    public Era eraOf(int eraValue);
    public List<Era> eras();
    public ChronoPeriod period(int years, int months, int days);
}

The code above shows key methods provided by AbstractChronology. These methods allow date creation, era handling, and calendar system queries. Each concrete chronology must implement these abstract methods.

Getting Chronology Information

Each chronology has a unique identifier and calendar type. These properties help identify the calendar system being used. The methods provide metadata about the chronology implementation.

Main.java
package com.zetcode;

import java.time.chrono.HijrahChronology;
import java.time.chrono.JapaneseChronology;
import java.time.chrono.MinguoChronology;
import java.time.chrono.ThaiBuddhistChronology;

public class Main {

    public static void main(String[] args) {
        
        System.out.println("Hijrah Chronology:");
        System.out.println("ID: " + HijrahChronology.INSTANCE.getId());
        System.out.println("Calendar Type: " + 
            HijrahChronology.INSTANCE.getCalendarType());
        
        System.out.println("\nJapanese Chronology:");
        System.out.println("ID: " + JapaneseChronology.INSTANCE.getId());
        System.out.println("Calendar Type: " + 
            JapaneseChronology.INSTANCE.getCalendarType());
        
        System.out.println("\nMinguo Chronology:");
        System.out.println("ID: " + MinguoChronology.INSTANCE.getId());
        System.out.println("Calendar Type: " + 
            MinguoChronology.INSTANCE.getCalendarType());
        
        System.out.println("\nThai Buddhist Chronology:");
        System.out.println("ID: " + ThaiBuddhistChronology.INSTANCE.getId());
        System.out.println("Calendar Type: " + 
            ThaiBuddhistChronology.INSTANCE.getCalendarType());
    }
}

This example demonstrates how to get identification information from different chronologies. Each calendar system has unique identifiers and types. These properties are useful for serialization and system integration.

Creating Dates in Different Calendars

AbstractChronology provides methods to create dates in various calendar systems. Dates can be created using year-month-day or year-day combinations. The methods support both proleptic years and era-based years.

Main.java
package com.zetcode;

import java.time.chrono.HijrahChronology;
import java.time.chrono.HijrahDate;
import java.time.chrono.JapaneseChronology;
import java.time.chrono.JapaneseDate;
import java.time.chrono.JapaneseEra;

public class Main {

    public static void main(String[] args) {
        
        // Create Hijrah date
        HijrahDate hijrahDate = HijrahChronology.INSTANCE
            .date(1445, 10, 15);
        System.out.println("Hijrah Date: " + hijrahDate);
        
        // Create Japanese date with era
        JapaneseDate japaneseDate = JapaneseChronology.INSTANCE
            .date(JapaneseEra.HEISEI, 30, 5, 1);
        System.out.println("Japanese Date: " + japaneseDate);
        
        // Create date from year-day
        HijrahDate hijrahYearDay = HijrahChronology.INSTANCE
            .dateYearDay(1445, 200);
        System.out.println("Hijrah Year-Day: " + hijrahYearDay);
        
        // Create date from temporal
        JapaneseDate fromTemporal = JapaneseChronology.INSTANCE
            .date(japaneseDate);
        System.out.println("From Temporal: " + fromTemporal);
    }
}

This example shows different ways to create dates in non-ISO calendar systems. The date methods support various parameter combinations. Each chronology handles date creation according to its specific rules.

Working with Eras

Many calendar systems use eras to mark significant periods. The AbstractChronology provides methods to work with era values. These include getting all eras and converting between era years and proleptic years.

Main.java
package com.zetcode;

import java.time.chrono.JapaneseChronology;
import java.time.chrono.JapaneseEra;
import java.time.chrono.MinguoChronology;
import java.time.chrono.MinguoEra;

public class Main {

    public static void main(String[] args) {
        
        // List all Japanese eras
        System.out.println("Japanese Eras:");
        JapaneseChronology.INSTANCE.eras().forEach(System.out::println);
        
        // Get era by value
        JapaneseEra heisei = JapaneseChronology.INSTANCE
            .eraOf(JapaneseEra.HEISEI.getValue());
        System.out.println("\nHeisei Era: " + heisei);
        
        // Convert between era year and proleptic year
        int prolepticYear = JapaneseChronology.INSTANCE
            .prolepticYear(JapaneseEra.HEISEI, 30);
        System.out.println("Proleptic Year: " + prolepticYear);
        
        // Minguo era example
        System.out.println("\nMinguo Era Before Common: " + 
            MinguoChronology.INSTANCE.eraOf(MinguoEra.BEFORE_ROC.getValue()));
    }
}

This example demonstrates era handling in different calendar systems. The Japanese calendar has multiple eras marking imperial reigns. Other calendars like Minguo have different era systems. The methods provide conversion between representations.

Checking Leap Years

Leap year rules vary between calendar systems. The isLeapYear method determines if a year is a leap year according to the chronology's rules. This is important for accurate date calculations.

Main.java
package com.zetcode;

import java.time.chrono.HijrahChronology;
import java.time.chrono.JapaneseChronology;
import java.time.chrono.ThaiBuddhistChronology;

public class Main {

    public static void main(String[] args) {
        
        // Check leap years in different calendars
        int year = 2024;
        
        System.out.println("ISO Leap Year: " + 
            ThaiBuddhistChronology.INSTANCE.isLeapYear(year - 543));
        
        System.out.println("Hijrah Leap Year: " + 
            HijrahChronology.INSTANCE.isLeapYear(year - 579));
        
        System.out.println("Japanese Leap Year: " + 
            JapaneseChronology.INSTANCE.isLeapYear(year));
        
        // Check multiple years
        System.out.println("\nHijrah Leap Years 1440-1445:");
        for (long y = 1440; y <= 1445; y++) {
            System.out.println(y + ": " + 
                HijrahChronology.INSTANCE.isLeapYear(y));
        }
    }
}

This example checks leap years in different calendar systems. Note the year adjustments for Thai Buddhist and Hijrah calendars. Each chronology implements its own leap year calculation rules. The results may differ between systems.

Creating ChronoPeriod Objects

AbstractChronology can create period objects specific to the calendar system. These periods represent amounts of time in years, months, and days. They are useful for date arithmetic within the chronology.

Main.java
package com.zetcode;

import java.time.chrono.HijrahChronology;
import java.time.chrono.HijrahDate;
import java.time.chrono.ChronoPeriod;
import java.time.chrono.JapaneseChronology;

public class Main {

    public static void main(String[] args) {
        
        // Create periods in different chronologies
        ChronoPeriod hijrahPeriod = HijrahChronology.INSTANCE
            .period(1, 6, 15);
        System.out.println("Hijrah Period: " + hijrahPeriod);
        
        ChronoPeriod japanesePeriod = JapaneseChronology.INSTANCE
            .period(2, 3, 10);
        System.out.println("Japanese Period: " + japanesePeriod);
        
        // Use period with dates
        HijrahDate hijrahDate = HijrahChronology.INSTANCE
            .date(1445, 1, 1);
        HijrahDate futureDate = hijrahDate.plus(hijrahPeriod);
        System.out.println("Future Hijrah Date: " + futureDate);
        
        // Negative period
        ChronoPeriod pastPeriod = HijrahChronology.INSTANCE
            .period(-1, -2, -5);
        HijrahDate pastDate = hijrahDate.plus(pastPeriod);
        System.out.println("Past Hijrah Date: " + pastDate);
    }
}

This example shows how to create and use periods in different calendar systems. Periods can be positive or negative for adding/subtracting time. The chronology ensures the period follows the calendar's specific rules when applied to dates.

Comparing Dates Across Calendars

Dates from different chronologies can be compared using the compareTo method. The comparison is based on the epoch-day value, allowing consistent ordering across calendar systems. This enables working with multiple calendars.

Main.java
package com.zetcode;

import java.time.chrono.HijrahChronology;
import java.time.chrono.HijrahDate;
import java.time.chrono.JapaneseChronology;
import java.time.chrono.JapaneseDate;
import java.time.chrono.ThaiBuddhistChronology;
import java.time.chrono.ThaiBuddhistDate;

public class Main {

    public static void main(String[] args) {
        
        // Create dates in different calendars
        HijrahDate hijrahDate = HijrahChronology.INSTANCE
            .date(1445, 1, 1);
        JapaneseDate japaneseDate = JapaneseChronology.INSTANCE
            .date(2023, 1, 1);
        ThaiBuddhistDate thaiDate = ThaiBuddhistChronology.INSTANCE
            .date(2566, 1, 1);
        
        // Compare dates
        System.out.println("Hijrah vs Japanese: " + 
            hijrahDate.compareTo(japaneseDate));
        System.out.println("Japanese vs Thai: " + 
            japaneseDate.compareTo(thaiDate));
        System.out.println("Thai vs Hijrah: " + 
            thaiDate.compareTo(hijrahDate));
        
        // Check if dates are equal in different calendars
        System.out.println("\nSame moment different calendars:");
        HijrahDate hd = HijrahChronology.INSTANCE.date(1445, 1, 1);
        JapaneseDate jd = JapaneseChronology.INSTANCE.date(hd);
        System.out.println("Equal: " + hd.equals(jd));
        System.out.println("Compare: " + hd.compareTo(jd));
    }
}

This example demonstrates comparing dates across different calendar systems. While the dates may represent the same moment, their calendar representations differ. The comparison is based on the underlying epoch-day value for consistency.

Source

Java AbstractChronology Class Documentation

In this article, we've covered the essential methods and features of the Java AbstractChronology class. Understanding these concepts is crucial for working with multiple calendar systems in Java applications.

Author

My name is Jan Bodnar, and I am a dedicated programmer with many years of experience in the field. I began writing programming articles in 2007 and have since authored over 1,400 articles and eight e-books. With more than eight years of teaching experience, I am committed to sharing my knowledge and helping others master programming concepts.

List all Java tutorials.