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

相关文章

最近看了一下学习资料,感觉进制转换其实还是挺有意思的,尤...
/*HashSet 基本操作 * --set:元素是无序的,存入和取出顺序不...
/*list 基本操作 * * List a=new List(); * 增 * a.add(inde...
/* * 内部类 * */ 1 class OutClass{ 2 //定义外部类的成员变...
集合的操作Iterator、Collection、Set和HashSet关系Iterator...
接口中常量的修饰关键字:public,static,final(常量)函数...