问题描述
我正在开发一个需要回历日期的应用程序,我使用了此处发布的内容。
I am developing an app that needs Hijri Date, I used what was published here converting gregorian to hijri date .
它对我有用,但是给出了错误的日期,例如,公历是2016年9月20日,它在希吉里变成了17/12/1437,那是错误的是1437年1月19日。
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.
问题出在哪里?
推荐答案
关于转换示例,在我看来,您正在寻找的是回历日历的Umalqura变体,它是沙特阿拉伯的官方日历。
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.
您发布的SO-链接指的是 强>。该库不支持umalqura,但支持 Hijri-calendar的四个算法变体(仅适合作为近似值)。我已经测试了所有四个受支持的变体。
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
如果您使用的是 Java-8 -平台,则可以使用以下解决方案。但是,如果您使用的是Android,并尝试使用反向端口ThreetenABP,则该操作将失败,因为其实现方式与Java-8不同:
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
如果您想要更多日历功能,例如其他变体或可变的开始时间(伊斯兰的开始时间是日落!),或者如果您在Android上运行,则可以使用我的库Time4J / A。 (或在Android上产生您想要的内容:
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]
最后一个示例是中午有效的转换。当将傍晚用作一天的开始时,请查阅javadoc的操作方法。
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.
自2016-09-07起更新:
在Android上具有非常不同的API的其他umalqura解决方案包括
Other possible umalqura solutions with very different APIs on Android include
- (Google似乎开始包含ICU4J)
- (感谢OP的评论,该库支持月份名称使用阿拉伯语和英语两种语言)
- 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)
这篇关于从公历转换为回历日期(Android Studio)时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!