java.time.chrono
Stay organized with collections
Save and categorize content based on your preferences.
Generic API for calendar systems other than the default ISO.
The main API is based around the calendar system defined in ISO-8601.
However, there are other calendar systems, and this package provides basic support for them.
The alternate calendars are provided in the java.time.chrono
package.
A calendar system is defined by the Chronology
interface,
while a date in a calendar system is defined by the ChronoLocalDate
interface.
It is intended that applications use the main API whenever possible, including code to read and write
from a persistent data store, such as a database, and to send dates and times across a network.
The "chrono" classes are then used at the user interface level to deal with localized input/output.
See ChronoLocalDate
for a full discussion of the issues.
Using non-ISO calendar systems in an application introduces significant extra complexity.
Ensure that the warnings and recommendations in ChronoLocalDate
have been read before
working with the "chrono" interfaces.
The supported calendar systems includes:
Example
This example lists todays date for all of the available calendars.
// Enumerate the list of available calendars and print todays date for each.
Set<Chronology> chronos = Chronology.getAvailableChronologies();
for (Chronology chrono : chronos) {
ChronoLocalDate date = chrono.dateNow();
System.out.printf(" %20s: %s%n", chrono.getId(), date.toString());
}
This example creates and uses a date in a named non-ISO calendar system.
// Print the Thai Buddhist date
ChronoLocalDate now1 = Chronology.of("ThaiBuddhist").dateNow();
int day = now1.get(ChronoField.DAY_OF_MONTH);
int dow = now1.get(ChronoField.DAY_OF_WEEK);
int month = now1.get(ChronoField.MONTH_OF_YEAR);
int year = now1.get(ChronoField.YEAR);
System.out.printf(" Today is %s %s %d-%s-%d%n", now1.getChronology().getId(),
dow, day, month, year);
// Print today's date and the last day of the year for the Thai Buddhist Calendar.
ChronoLocalDate first = now1
.with(ChronoField.DAY_OF_MONTH, 1)
.with(ChronoField.MONTH_OF_YEAR, 1);
ChronoLocalDate last = first
.plus(1, ChronoUnit.YEARS)
.minus(1, ChronoUnit.DAYS);
System.out.printf(" %s: 1st of year: %s; end of year: %s%n", last.getChronology().getId(),
first, last);
This example creates and uses a date in a specific ThaiBuddhist calendar system.
// Print the Thai Buddhist date
ThaiBuddhistDate now1 = ThaiBuddhistDate.now();
int day = now1.get(ChronoField.DAY_OF_MONTH);
int dow = now1.get(ChronoField.DAY_OF_WEEK);
int month = now1.get(ChronoField.MONTH_OF_YEAR);
int year = now1.get(ChronoField.YEAR);
System.out.printf(" Today is %s %s %d-%s-%d%n", now1.getChronology().getId(),
dow, day, month, year);
// Print today's date and the last day of the year for the Thai Buddhist Calendar.
ThaiBuddhistDate first = now1
.with(ChronoField.DAY_OF_MONTH, 1)
.with(ChronoField.MONTH_OF_YEAR, 1);
ThaiBuddhistDate last = first
.plus(1, ChronoUnit.YEARS)
.minus(1, ChronoUnit.DAYS);
System.out.printf(" %s: 1st of year: %s; end of year: %s%n", last.getChronology().getId(),
first, last);
Package specification
Unless otherwise noted, passing a null argument to a constructor or method in any class or interface
in this package will cause a NullPointerException
to be thrown.
The Javadoc "@param" definition is used to summarise the null-behavior.
The "@throws NullPointerException
" is not explicitly documented in each method.
All calculations should check for numeric overflow and throw either an ArithmeticException
or a DateTimeException
.
Interfaces
ChronoLocalDate |
A date without time-of-day or time-zone in an arbitrary chronology, intended
for advanced globalization use cases. |
ChronoLocalDateTime<D extends ChronoLocalDate> |
A date-time without a time-zone in an arbitrary chronology, intended
for advanced globalization use cases. |
Chronology |
A calendar system, used to organize and identify dates. |
ChronoPeriod |
A date-based amount of time, such as '3 years, 4 months and 5 days' in an
arbitrary chronology, intended for advanced globalization use cases. |
ChronoZonedDateTime<D extends ChronoLocalDate> |
A date-time with a time-zone in an arbitrary chronology,
intended for advanced globalization use cases. |
Era |
An era of the time-line. |
Classes
Enums
IsoEra |
An era in the ISO calendar system. |
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2024-07-10 UTC.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2024-07-10 UTC."],[[["\u003cp\u003eThis package provides support for non-ISO calendar systems for advanced globalization use cases.\u003c/p\u003e\n"],["\u003cp\u003eWhile the primary API uses the ISO calendar system, this package offers alternatives through the \u003ccode\u003ejava.time.chrono\u003c/code\u003e package.\u003c/p\u003e\n"],["\u003cp\u003eApplications should primarily use the main ISO-based API and leverage the "chrono" classes for localized user interface interactions.\u003c/p\u003e\n"],["\u003cp\u003eUsing non-ISO calendars adds complexity, so review the \u003ccode\u003eChronoLocalDate\u003c/code\u003e documentation for warnings and recommendations before implementation.\u003c/p\u003e\n"],["\u003cp\u003eSupported calendar systems include Hijrah, Japanese, Minguo, and Thai Buddhist.\u003c/p\u003e\n"]]],["This package offers support for calendar systems beyond the default ISO-8601, using the `java.time.chrono` package. Key interfaces include `Chronology` for defining a calendar and `ChronoLocalDate` for dates within it. Applications should primarily use the main API, employing \"chrono\" classes for localized user interface needs. The package supports Hijrah, Japanese, Minguo, and Thai Buddhist calendars. `Chronology.getAvailableChronologies()` allows enumeration, and `Chronology.of()` is used to retrieve a named calendar. Various example are given to use these capabilities.\n"],null,["Generic API for calendar systems other than the default ISO.\n\n\nThe main API is based around the calendar system defined in ISO-8601.\nHowever, there are other calendar systems, and this package provides basic support for them.\nThe alternate calendars are provided in the [java.time.chrono](../../../../reference/java/time/chrono/package-summary.html) package.\n\n\nA calendar system is defined by the [Chronology](../../../../reference/java/time/chrono/Chronology.html) interface,\nwhile a date in a calendar system is defined by the [ChronoLocalDate](../../../../reference/java/time/chrono/ChronoLocalDate.html) interface.\n\n\nIt is intended that applications use the main API whenever possible, including code to read and write\nfrom a persistent data store, such as a database, and to send dates and times across a network.\nThe \"chrono\" classes are then used at the user interface level to deal with localized input/output.\nSee [ChronoLocalDate](../../../../reference/java/time/chrono/ChronoLocalDate.html)\nfor a full discussion of the issues.\n\n\nUsing non-ISO calendar systems in an application introduces significant extra complexity.\nEnsure that the warnings and recommendations in `ChronoLocalDate` have been read before\nworking with the \"chrono\" interfaces.\n\n\nThe supported calendar systems includes:\n\n- [Hijrah calendar](../../../../reference/java/time/chrono/HijrahChronology.html)\n- [Japanese calendar](../../../../reference/java/time/chrono/JapaneseChronology.html)\n- [Minguo calendar](../../../../reference/java/time/chrono/MinguoChronology.html)\n- [Thai Buddhist calendar](../../../../reference/java/time/chrono/ThaiBuddhistChronology.html)\n\nExample\n\n\nThis example lists todays date for all of the available calendars. \n\n```\n // Enumerate the list of available calendars and print todays date for each.\n Set\u003cChronology\u003e chronos = Chronology.getAvailableChronologies();\n for (Chronology chrono : chronos) {\n ChronoLocalDate date = chrono.dateNow();\n System.out.printf(\" %20s: %s%n\", chrono.getId(), date.toString());\n }\n \n```\n\n\nThis example creates and uses a date in a named non-ISO calendar system. \n\n```\n // Print the Thai Buddhist date\n ChronoLocalDate now1 = Chronology.of(\"ThaiBuddhist\").dateNow();\n int day = now1.get(ChronoField.DAY_OF_MONTH);\n int dow = now1.get(ChronoField.DAY_OF_WEEK);\n int month = now1.get(ChronoField.MONTH_OF_YEAR);\n int year = now1.get(ChronoField.YEAR);\n System.out.printf(\" Today is %s %s %d-%s-%d%n\", now1.getChronology().getId(),\n dow, day, month, year);\n // Print today's date and the last day of the year for the Thai Buddhist Calendar.\n ChronoLocalDate first = now1\n .with(ChronoField.DAY_OF_MONTH, 1)\n .with(ChronoField.MONTH_OF_YEAR, 1);\n ChronoLocalDate last = first\n .plus(1, ChronoUnit.YEARS)\n .minus(1, ChronoUnit.DAYS);\n System.out.printf(\" %s: 1st of year: %s; end of year: %s%n\", last.getChronology().getId(),\n first, last);\n \n```\n\n\nThis example creates and uses a date in a specific ThaiBuddhist calendar system. \n\n```\n // Print the Thai Buddhist date\n ThaiBuddhistDate now1 = ThaiBuddhistDate.now();\n int day = now1.get(ChronoField.DAY_OF_MONTH);\n int dow = now1.get(ChronoField.DAY_OF_WEEK);\n int month = now1.get(ChronoField.MONTH_OF_YEAR);\n int year = now1.get(ChronoField.YEAR);\n System.out.printf(\" Today is %s %s %d-%s-%d%n\", now1.getChronology().getId(),\n dow, day, month, year);\n\n // Print today's date and the last day of the year for the Thai Buddhist Calendar.\n ThaiBuddhistDate first = now1\n .with(ChronoField.DAY_OF_MONTH, 1)\n .with(ChronoField.MONTH_OF_YEAR, 1);\n ThaiBuddhistDate last = first\n .plus(1, ChronoUnit.YEARS)\n .minus(1, ChronoUnit.DAYS);\n System.out.printf(\" %s: 1st of year: %s; end of year: %s%n\", last.getChronology().getId(),\n first, last);\n \n```\n\nPackage specification\n\n\nUnless otherwise noted, passing a null argument to a constructor or method in any class or interface\nin this package will cause a [NullPointerException](../../../../reference/java/lang/NullPointerException.html) to be thrown.\nThe Javadoc \"@param\" definition is used to summarise the null-behavior.\nThe \"@throws [NullPointerException](../../../../reference/java/lang/NullPointerException.html)\" is not explicitly documented in each method.\n\n\nAll calculations should check for numeric overflow and throw either an [ArithmeticException](../../../../reference/java/lang/ArithmeticException.html)\nor a [DateTimeException](../../../../reference/java/time/DateTimeException.html).\n\nInterfaces \n\n|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|------------------------------------------------------------------------------------------------------------------------------------------------|\n| [ChronoLocalDate](../../../../reference/java/time/chrono/ChronoLocalDate.html) | A date without time-of-day or time-zone in an arbitrary chronology, intended for advanced globalization use cases. |\n| [ChronoLocalDateTime](../../../../reference/java/time/chrono/ChronoLocalDateTime.html)\\\u003cD extends [ChronoLocalDate](../../../../reference/java/time/chrono/ChronoLocalDate.html)\\\u003e | A date-time without a time-zone in an arbitrary chronology, intended for advanced globalization use cases. |\n| [Chronology](../../../../reference/java/time/chrono/Chronology.html) | A calendar system, used to organize and identify dates. |\n| [ChronoPeriod](../../../../reference/java/time/chrono/ChronoPeriod.html) | A date-based amount of time, such as '3 years, 4 months and 5 days' in an arbitrary chronology, intended for advanced globalization use cases. |\n| [ChronoZonedDateTime](../../../../reference/java/time/chrono/ChronoZonedDateTime.html)\\\u003cD extends [ChronoLocalDate](../../../../reference/java/time/chrono/ChronoLocalDate.html)\\\u003e | A date-time with a time-zone in an arbitrary chronology, intended for advanced globalization use cases. |\n| [Era](../../../../reference/java/time/chrono/Era.html) | An era of the time-line. |\n\nClasses \n\n|--------------------------------------------------------------------------------------|---------------------------------------------------------------------------------------|\n| [AbstractChronology](../../../../reference/java/time/chrono/AbstractChronology.html) | An abstract implementation of a calendar system, used to organize and identify dates. |\n| [IsoChronology](../../../../reference/java/time/chrono/IsoChronology.html) | The ISO calendar system. |\n\nEnums \n\n|--------------------------------------------------------------|------------------------------------|\n| [IsoEra](../../../../reference/java/time/chrono/IsoEra.html) | An era in the ISO calendar system. |"]]