javaandroiddatecalendarhijri

How to convert from Hijri Date to Georgian Date and vice versa


I am looking for a way to calculate for example how many days it is from now, to a specific Date AND that I can use to determine whether I am in a specific time period (eg. Muharram +- 5days) or not

I have been looking for over 10hours now, and the best things I found were the "HijrahDate" library "java.time.chrono.HijrahDate" and something called "Joda Date", which I had difficulties to use.


Solution

  • Which variation of Hijri calendar do you want to use?

    If you opt for the official calendar of Saudi-Arabia then the solution based on java.time.HijrahDate would work. But this class requires at least API level 26 on Android. Example:

    HijrahDate hd1 = HijrahDate.now();
    HijrahDate hd2 = HijrahDate.from(LocalDate.of(2020, 5, 1));
    long days = ChronoUnit.DAYS.between(hd1, hd2);
    

    There are also comparison methods like isAfter() or isBefore() inherited from the interface ChronoLocalDate and standard plus()-methods in order to determine if your date is in a specific time period.

    Backport of java.time:

    There is also a backport called ThreetenABP for lower Android-versions. But be aware of the pitfall that its implementation of HijrahDate is different and does NOT use the calendar of Saudi-Arabia (so you have to tolerate differences in date conversion).

    About Joda-Time:

    If you opt for that (rather outdated) library then you should choose the library version adapted for android. However, it does not support the calendar of Saudi-Arabia, too, but offers four different other variations. You would need to specify the algorithmic leap year pattern.

    ICU4J (embedded in Android):

    Its class IslamicCalendar offers a style similar to old calendar classes java.util.Calendar and also several variants including that of Saudi-Arabia. The minimum required API level is 24.

    Time4A:

    That is a library written by myself (as adaptation of Time4J for Android). It offers the class HijriCalendar with several variations including the Joda-variants but also including the calendar of Saudi-Arabia (variant ummalqura). It offers all needed features like date arithmetic (by plus()- or minus()-method), date comparison (by isAfter() etc.). Example:

    String variant = HijriCalendar.VARIANT_UMALQURA;
    StartOfDay startOfDay = StartOfDay.definedBy(SolarTime.ofMecca().sunset());
    HijriCalendar today = HijriCalendar.nowInSystemTime(variant, startOfDay);
    HijriCalendar hcal = // gregorian to Hijri conversion
        PlainDate.of(2020, 5, 1).transform(HijriCalendar.class, variant);
    long days = CalendarDays.between(today, hcal).getAmount();
    

    Features like sunset as start of day are not supported by other libraries. Example for your Muharram +- 5days-request might look like:

    CalendarDays tolerance = CalendarDays.of(5);
    HijriCalendar htemp = today.with(HijriCalendar.MONTH_OF_YEAR, HijriMonth.MUHARRAM);
    HijriCalendar h1 = htemp.with(HijriCalendar.DAY_OF_MONTH.minimized()).minus(tolerance);
    HijriCalendar h2 = htemp.with(HijriCalendar.DAY_OF_MONTH.maximized()).plus(tolerance);
    boolean inTimePeriod = !(today.isBefore(h1) || today.isAfter(h2));