以下内容如何正确:
LocalDate gregorianDate = LocalDate.parse(gregorianString, dateFormatter);
HijrahDate islamicDate = HijrahDate.from(gregorianDate);
我认为回历日在日落结束,而不是在午夜结束,所以应该需要一个 gregorianDateTime 。
以下内容如何正确:
LocalDate gregorianDate = LocalDate.parse(gregorianString, dateFormatter);
HijrahDate islamicDate = HijrahDate.from(gregorianDate);
我认为回历日在日落结束,而不是在午夜结束,所以应该需要一个 gregorianDateTime 。
你是对的,回历日历从前一个西方日子的日落开始。您也可以说标准 Java 不支持它。
为什么?
我在旧的 Threeten-archives 中搜索过,但一无所获。但是,-API 的主要开发者和架构师曾在祖先 Joda-Timejava.time
的文档中说过以下一句话:
此实现完全按照 ISO 年表将一天定义为午夜到午夜。这个正确的一天开始时间是在前一天的日落时分,但是这不能很容易地建模并且被忽略了。
我的猜测是:同样的动机也负责为什么java.time
在转换到或从伊斯兰历法时不考虑一天中的时间。实施起来比较困难,主要是因为需要天文计算。
如何处理这种不足?
在标准 Java 的范围内,建议用户尽可能忽略一天中的时间。所以请记住,这样的转换是无时间的抽象。当然,如果要确定当前日期(间接涉及一天中的时间和民用时区),那么这样的转换或多或少是错误的!
如果您不想忽略日落作为一天的开始......
...然后您可以使用我的库Time4J(而且我不知道任何其他可以将日落作为一天开始的库)。注意HijriCalendar类的文档。从瞬间/即时到回历日历日期的通用转换示例:
// the geographic location
SolarTime meccaTime = SolarTime.ofLocation(21.4225, 39.826111);
// or even simple: meccaTime = SolarTime.ofMecca()
// the moment to be converted
Moment now = SystemClock.currentTime();
// alternative using an explicit gregorian date:
// now = PlainDate.of(2019, 5, 26).atTime(18, 45).inStdTimezone();
// alternative using modern Java:
// now = Moment.from(Instant.now());
// alternative using outdated old API:
// java.util.Date instant = new java.util.Date();
// now = TemporalType.JAVA_UTIL_DATE.translate(instant);
// the conversion
HijriCalendar hcal = now.toGeneralTimestamp(
HijriCalendar.family(),
HijriCalendar.VARIANT_UMALQURA, // attention: there is no single islamic calendar
Timezone.ofSystem().getID(), // or use something like: ()-> "Europe/London"
StartOfDay.definedBy(meccaTime.sunset()));
Time4J 还包含一个格式引擎,它能够以多种方式解析公历日期或回历日期时间。在基于ChronoFormatter的格式化和解析过程中,也可以考虑日落时的一天开始。
还支持从 Hijri 日历日期到时刻的反向转换。例子:
HijriCalendar hcal = ...;
Moment m =
hcal.atTime(18, 45).in(Timezone.ofSystem(), StartOfDay.definedBy(meccaTime.sunset()));
Java 文档中提到 LocalDate 类不考虑时间。
参考:https ://docs.oracle.com/javase/8/docs/api/java/time/LocalDate.html
此类不存储或表示时间或时区。相反,它是对日期的描述,用于生日。
考虑根据您的要求使用 LocalDateTime。