微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

java-如何在没有时间的情况下将公历日期转换为伊斯兰(Hijri)日期?

参见SO 15728744SO 51058875.

以下内容如何正确:

LocalDate gregorianDate = LocalDate.parse(gregorianString,dateFormatter);
HijrahDate islamicDate = HijrahDate.from(gregorianDate);

我以为回教日在日落而不是午夜结束,因此应该使用gregorianDateTime.

最佳答案
您是对的,回历日历是在上一个西方日的日落开始的日子.您还可以说标准Java不支持它.

为什么?

搜索了旧的Threeten存档,但一无所获.但是,java.time-API的主要开发人员和架构师曾在documentation of the ancestor Joda-Time的以下句子中指出:

This implementation defines a day as midnight to midnight exactly as
per the ISO chronology. This correct start of day is at sunset on the
prevIoUs day,however this cannot readily be modelled and has been
ignored.

我的推测是:同样的动机也是为什么java.time在转换为伊斯兰日历或从伊斯兰日历转换时不考虑一天中的时间的原因.主要由于需要天文计算而难以实施.

如何处理这种不足?

在标准Java范围内,建议用户尽可能忽略一天中的时间.因此,请记住,此类转换是无时间的抽象.当然,如果要确定当前日期(间接涉及一天中的时间和民用时区),那么这种转换或多或少都是错误的!

如果您不想忽略日落作为一天的开始…

…那么您可以使用我的图书馆Time4J(而且我不知道还有其他任何可以处理日落的lib).请注意类HijriCalendar的文档.从瞬间/即时到回历日历日期的generic conversion示例:

// 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的格式化和解析过程中,也可以考虑日落时的开始时间.

支持从回历日历日期到某个时刻的反向转换.例:

HijriCalendar hcal = ...;
Moment m = 
  hcal.atTime(18,45).in(Timezone.ofSystem(),StartOfDay.definedBy(meccaTime.sunset()));

原文地址:https://www.jb51.cc/java/532918.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐