I take a year number from user. How can convert it to hijri year number? Better using not jodatime but time4j or java 8.
Example: I need from 2016 to have hijri year number.
I have found this post, Is there any library or algorithm for Persian (Shamsi or Jalali) calendar in Android?
System.out.println(jalali.transform(PlainDate.class)); // 2015-03-25
But method transform doesn't work.
Also I have found this post to convert hijri to gregorian: Get a gregorian date from Hijri date strings
But I need from gregorian to hijri.
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