問題描述
我正在尋找一種方法來計算例如從現在到特定日期的天數,并且我可以使用它來確定我是否處于特定時間段(例如穆哈拉姆 +- 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);
還有繼承自接口ChronoLocalDate
的isAfter()
或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));
這篇關于如何從回歷日期轉換為格魯吉亞日期,反之亦然的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!