Non-ISO Calendar Systems
Description
LocalDate uses the ISO calendar system, which is the Gregorian calendar.
Java Date-Time API also support other calendars such as Thai Buddhist calendar, Hijrah calendar, Minguo calendar, and Japanese calendar.
The non-ISO calendar-related classes are
defined in the java.time.chrono
package.
For each available non-ISO calendar system there is a custom-named Chronology and custom-named Date class.
Custom-named Chronology class represents the calendar system whereas custom-named Date class represents a date in the custom calendar system.
Each custom-named Chronology class contains an INSTANCE constant that represents a singleton instance of that class.
Example
The following code shows to get the current date in the Thai Buddhist calendar:
import java.time.chrono.ThaiBuddhistChronology;
import java.time.chrono.ThaiBuddhistDate;
/* ww w .ja v a 2 s. co m*/
public class Main {
public static void main(String[] args) {
ThaiBuddhistChronology thaiBuddhistChrono = ThaiBuddhistChronology.INSTANCE;
ThaiBuddhistDate now = thaiBuddhistChrono.dateNow();
ThaiBuddhistDate now2 = ThaiBuddhistDate.now();
System.out.println("Current Date in Thai Buddhist: " + now);
System.out.println("Current Date in Thai Buddhist: " + now2);
}
}
The code above generates the following result.
Example 2
We can convert dates in one calendar system to another
using from()
method.
The following code shows how to convert ISO date to Thai Buddhist date and vice versa.
import java.time.LocalDate;
import java.time.chrono.ThaiBuddhistDate;
//from w w w. j av a 2 s .com
public class Main {
public static void main(String[] args) {
ThaiBuddhistDate thaiBuddhistNow = ThaiBuddhistDate.now();
LocalDate isoNow = LocalDate.now();
System.out.println("Thai Buddhist Current Date: " + thaiBuddhistNow);
System.out.println("ISO Current Date: " + isoNow);
// Convert Thai Buddhist date to ISO date and vice versa
ThaiBuddhistDate thaiBuddhistNow2 = ThaiBuddhistDate.from(isoNow);
LocalDate isoNow2 = LocalDate.from(thaiBuddhistNow);
System.out.println("Thai Buddhist Current Date from ISO: "
+ thaiBuddhistNow2);
System.out.println("ISO Current Date from Thai Buddhist: " + isoNow2);
}
}
The code above generates the following result.