为什么我不能在几分钟或几小时内获得java.time中的持续时间? [英] Why can't I get a duration in minutes or hours in java.time?

查看:319
本文介绍了为什么我不能在几分钟或几小时内获得java.time中的持续时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

持续时间<新 JSR 310 日期的/ code> 课程API( java.time包)在Java 8及更高版本中,javadoc说:

Of the Duration class in the new JSR 310 date API (java.time package) available in Java 8 and later, the javadoc says :


这个类用秒和$ b $模拟一个数量或时间量b纳秒。 可以使用其他基于持续时间的单位访问,例如
作为分钟和小时
。此外,可以使用DAYS单位并将其视为完全等于24小时,因此忽略了日光储蓄效应。

This class models a quantity or amount of time in terms of seconds and nanoseconds. It can be accessed using other duration-based units, such as minutes and hours.In addition, the DAYS unit can be used and is treated as exactly equal to 24 hours, thus ignoring daylight savings effects.

那么,为什么以下代码会崩溃?

So, why does the following code crash ?

Duration duration = Duration.ofSeconds(3000);
System.out.println(duration.get(ChronoUnit.MINUTES));

这会引发 UnsupportedTemporalTypeException

java.time.temporal.UnsupportedTemporalTypeException: Unsupported unit: Minutes
    at java.time.Duration.get(Duration.java:537)

那么从持续时间对象中提取分钟和小时的推荐方法是什么?我们是否必须从秒数开始计算?为什么以这种方式实现?

So what is the recommended way to extract minutes and hours from a duration object ? Do we have to make the calculation ourselves from the number of seconds ? Why was it implemented that way ?

推荐答案

为什么这样实现?

其他答案涉及允许查询小时/分钟的 toXxx()方法。我将尝试处理原因。

Other answers deal with the toXxx() methods that allow the hours/minutes to be queried. I'll try to deal with the why.

TemporalAmount 界面和获取( TemporalUnit)方法在这个过程中相当晚。我个人并不完全相信我们有足够的证据证明在该区域设计工作的正确方法,但是稍微扭曲了一下以添加 TemporalAmount 。我相信在这样做时我们会稍微混淆API。

The TemporalAmount interface and get(TemporalUnit) method was added fairly late in the process. I personally was not entirely convinced that we had enough evidence of the right way to work the design in that area, but was slightly arm-twisted to add TemporalAmount. I believe that in doing so we slightly confused the API.

事后看来,我认为 TemporalAmount 包含了权利方法,但我相信 get(TemporalUnit)应该有不同的方法名称。原因是 get(TemporalUnit)本质上是一种框架级方法 - 它不是为日常使用而设计的。不幸的是,方法名称 get 并不意味着这一点,导致在<$上调用 get(ChronoUnit.MINUTES)等错误c $ c>持续时间

In hindsight, I believe that TemporalAmount contains the right methods, but I believe that get(TemporalUnit) should have had a different method name. The reason is that get(TemporalUnit) is essentially a framework-level method - it is not designed for day-today use. Unfortunately the method name get does not imply this, resulting in bugs like calling get(ChronoUnit.MINUTES) on Duration.

因此,想到获取(TemporalUnit)的方式想象一个低级框架,将金额视为 Map< TemporalUnit,Long> 其中持续时间地图,大小为2,密钥为 SECONDS NANOS

So, the way to think of get(TemporalUnit) is to imagine a low-level framework viewing the amount as a Map<TemporalUnit, Long> where Duration is a Map of size two with keys of SECONDS and NANOS.

以同样的方式,期间从低级框架中被视为地图,大小为3 - DAYS MONTHS (幸运的是,错误的可能性较小)。

In the same, way, Period is viewed from the low-level frameworks as a Map of size three - DAYS, MONTHS and YEARS (which fortunately has less chance of errors).

总的来说,应用程序代码的最佳建议是忽略方法得到(TemporalUnit)。使用 getSeconds() getNano() toHours() toMinutes()而不是。

Overall, the best advice for application code is to ignore the method get(TemporalUnit). Use getSeconds(), getNano(), toHours() and toMinutes() instead.

最后,从一个方法获得hh:mm:ss 持续时间是这样做的:

Finally, one way to get "hh:mm:ss" from a Duration is to do:

LocalTime.MIDNIGHT.plus(duration).format(DateTimeFormatter.ofPattern("HH:mm:ss"))

一点也不漂亮,但是它的工作时间不到一天。

Not pretty at all, but it does work for durations less than one day.

JDK-8142936 问题现已在Java 9中实施,添加以下方法来访问持续时间的每个部分。

JDK-8142936 issue now implemented in Java 9, adding the following methods to access each part of a Duration.


  • toDaysPart

  • toHoursPart

  • toMinutesPart

  • toSecondsPart

  • toMillisPart

  • toNanosPart

  • toDaysPart
  • toHoursPart
  • toMinutesPart
  • toSecondsPart
  • toMillisPart
  • toNanosPart

这篇关于为什么我不能在几分钟或几小时内获得java.time中的持续时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆