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

心不动则不痛 提交于 2019-12-09 01:44:14

问题


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 when the Gregorian is 20/9/2016 it became 17/12/1437 IN Hijri, and that's wrong it should be 19/12/1437.

Where is the problem ?


回答1:


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)


来源:https://stackoverflow.com/questions/39614504/wrong-in-converting-from-gregorian-to-hijri-date-android-studio

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!