如何从回历日期转换为格鲁吉亚日期,反之亦然

2022-01-11 00:00:00 date calendar android java hijri

我正在寻找一种方法来计算例如从现在到特定日期的天数,并且我可以使用它来确定我是否处于特定时间段(例如穆哈拉姆 +- 5 天)

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

我已经找了 10 多个小时了,我找到的最好的东西是HijrahDate".库java.time.chrono.HijrahDate"还有一个叫Joda Date"的东西,我很难用.

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.

推荐答案

你想使用哪种回历?

如果您选择沙特阿拉伯的官方日历,则该解决方案基于 java.time.HijrahDate 会起作用.但是这个类在 Android 上至少需要 API 级别 26.示例:

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);

还有继承自接口ChronoLocalDateisAfter()isBefore()等比较方法和标准的plus()方法为了确定您的日期是否在特定时间段内.

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.

java.time的向后移植:

Backport of java.time:

还有一个名为 ThreetenABP 的反向移植,用于较低的 Android 版本.但请注意,它的 HijrahDate 实现是不同的,并且不使用沙特阿拉伯的日历(因此您必须容忍日期转换的差异).

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).

关于 Joda-Time:

如果您选择那个(相当过时的)库,那么您应该选择适用于 android 的库版本.但是,它也不支持沙特阿拉伯的日历,但提供了四种不同的其他变体.您需要指定算法 闰年模式.

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(嵌入在 Android 中):

它的类 IslamicCalendar 提供类似于旧日历类的样式 java.util.Calendar 以及几个变体,包括沙特阿拉伯的变体.要求的最低 API 级别为 24.

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:

那是我自己写的一个库(作为Time4J for Android的改编).它为类 HijriCalendar 提供了多种变体,包括 Joda 变体还包括沙特阿拉伯的日历(变体 ummalqura).它提供了所有需要的功能,如日期算术(通过 plus()- 或 minus()- 方法)、日期比较(通过 isAfter() 等).示例:

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();

其他库不支持日落作为一天的开始等功能.您的 Muharram +- 5days-request 示例可能如下所示:

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));

相关文章