Wrong in Converting from Gregorian to Hijri Date (Android Studio)

前端 未结 1 1656
南笙
南笙 2021-01-06 23:39

I am developing an app that needs Hijri Date, I used what was published here converting gregorian to hijri date .

It works for me but it gave wrong date for example

相关标签:
1条回答
  • 2021-01-07 00:29

    Regarding your conversion example, it seems to me you are looking for the Umalqura-variant of Hijri-calendar which is the official calendar of Saudi-Arabia.

    The SO-link you posted refers to Joda-Time. This library does not support umalqura but four algorithmic variants of Hijri-calendar (only suitable as approximation). I have tested all four supported variants. None is for you.

    Chronology iso = ISOChronology.getInstanceUTC();
    Chronology hijri =
        IslamicChronology.getInstance(DateTimeZone.UTC, IslamicChronology.LEAP_YEAR_INDIAN);
    LocalDate todayIso = new LocalDate(2016, 9, 20, iso);
    LocalDate todayHijri = new LocalDate(todayIso.toDateTimeAtStartOfDay(), hijri);
    System.out.println("joda=" + todayHijri);
    // LEAP_YEAR_15_BASED => 1437-12-16
    // LEAP_YEAR_16_BASED => 1437-12-16
    // LEAP_YEAR_HABASH_AL_HASIB => 1437-12-17
    // LEAP_YEAR_INDIAN => 1437-12-17
    

    If you are operating on Java-8-platform then you can use the following solution. However, if you are on Android and try to use the backport ThreetenABP then that will fail because its implementation deviates from Java-8:

    HijrahDate hd = HijrahDate.from(LocalDate.of(2016, 9, 20));
    System.out.println("java.time=" + hd); // Hijrah-umalqura AH 1437-12-19
    

    If you want more calendar features like other variants or variable start of day (islamic days start at sunset!) or if you are operating on Android then you can use my library Time4J/A. The HijriCalendar of Time4J (or Time4A on Android) yields what you want:

    System.out.println(
      PlainDate.of(2016, 9, 20)
        .transform(HijriCalendar.class, HijriCalendar.VARIANT_UMALQURA)); 
    // AH-1437-12-19[islamic-umalqura]
    

    Note that the last example is a conversion valid at noon time. When using evening as start of day then please consult the javadoc how to do that.

    Update from 2016-09-07:

    Other possible umalqura solutions with very different APIs on Android include

    • Android developer preview 24 (Google seems to begin to include ICU4J)
    • Calendar-adaptation of msarhan (thanks to comment of OP, this library supports two languages arabic and english for month names)
    0 讨论(0)
提交回复
热议问题