The sentence "But method transform doesn't work." should probably be read "does not compile".
Reason is that the HijriCalendar
represents a family of different variants (for example Umalqura). Technical background: This class inherits from CalendarVariant
instead of Calendrical
. Therefore the compiler forces you to use a transform()
-method with TWO arguments. The second argument specifies the variant of HijriCalendar
. Example:
PlainDate today = SystemClock.inLocalView().today();
HijriCalendar umalqura =
today.transform(HijriCalendar.class, HijriCalendar.VARIANT_UMALQURA);
HijriCalendar westIslamicCivil =
today.transform(HijriCalendar.class, HijriAlgorithm.WEST_ISLAMIC_CIVIL);
System.out.println(umalqura); // AH-1437-07-23[islamic-umalqura]
System.out.println(westIslamicCivil); // AH-1437-07-22[islamic-civil]
This example also demonstrates why it is important to specify the variant. You see a difference of one day (for the same gregorian date)! Actually Time4J supports 11 variants. The umalquara-variant is the official calendar of Saudi-Arabia while the 8 algorithmic variants should rather be interpreted as approximations and have a wider validity range.
Once you have an instance of HijriCalendar
you can easily get the hijri year:
int hijriYear = umalqura.getYear();
System.out.println("Year (hirji-umalqura): " + hijriYear); // 1437