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

您是对的,回历日历是在上一个西方日的日落开始的日子.您还可以说标准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()));

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值