【问题标题】:Subtleties between Java Period and DurationJava Period 和 Duration 之间的细微差别
【发布时间】:2017-04-28 15:15:54
【问题描述】:

我不确定我是否了解 Java PeriodDuration 之间的微妙之处。

当我阅读 Oracle 的 explanation 时,它说我可以找出这样的生日有多少天(使用他们使用的示例日期):

LocalDate today = LocalDate.now();
LocalDate birthday = LocalDate.of(1960, Month.JANUARY, 1);
Period birthdayPeriod = Period.between(birthday, today);
int daysOld = birthdayPeriod.getDays();

但正如他们所指出的,这并没有考虑到你出生的时区和你现在所在的时区。但这是一台计算机,我们可以精确,对吧?那么我会使用Duration吗?

ZoneId bornIn = ZoneId.of("America/New_York");
ZonedDateTime born = ZonedDateTime.of(1960, Month.JANUARY.getValue(), 1, 2, 34, 56, 0, bornIn);
ZonedDateTime now = ZonedDateTime.now();
Duration duration = Duration.between(born, now);
long daysPassed = duration.toDays();

现在实际时间是准确的,但如果我理解正确,这些天可能无法正确代表日历日,例如与 DST 等。

那么我该怎么做才能根据我的时区获得准确的答案?我唯一能想到的就是回到使用LocalDate,但首先从ZonedDateTime 值规范化时区,然后使用Duration

ZoneId bornIn = ZoneId.of("America/New_York");
ZonedDateTime born = ZonedDateTime.of(1960, Month.JANUARY.getValue(), 1, 2, 34, 56, 0, bornIn);
ZonedDateTime now = ZonedDateTime.now();
ZonedDateTime nowNormalized=now.withZoneSameInstant(born.getZone());
Period preciseBirthdayPeriod = Period.between(born.toLocalDate(), nowNormalized.toLocalDate());
int preciseDaysOld = preciseBirthdayPeriod.getDays();

但这似乎真的很复杂,只是为了得到一个准确的答案。

【问题讨论】:

  • 您所说的“准确答案”到底是什么意思?假设您在两天前的凌晨 12:30 出生,但昨天有一个 DST 转换,所以这一天有 25 个小时,现在是凌晨 12:30。你已经活了 2 个日历日,但是 49 小时。那时你想要什么“精确”的答案?如果您可以准确地指定您期望的答案,那么计算它应该不会太难......但您确实需要非常具体。 (这甚至没有考虑不同的区域。)另外,请使用预览确保您的代码在发布时格式正确。
  • 期间是日历日之间的跨度,与一天中的时间无关。如果您想要比这更高的精度,则不应使用 Period。

标签: java time duration


【解决方案1】:

您对 Java-8 类 PeriodDuration 的分析或多或少是正确的。

  • java.time.Period 类仅限于日历日期精度。
  • java.time.Duration 类仅处理秒(和纳秒)精度,但始终将天视为等于 24 小时 = 86400 秒。

通常在计算一个人的年龄时忽略时钟精度或时区就完全足够了,因为像护照这样的个人文件不会记录一个人出生的确切时间。如果是这样,那么Period-class 就完成了它的工作(但请小心处理它的方法,如getDays() - 见下文)。

但您需要更高的精度并根据本地字段描述结果并考虑时区。嗯,Duration 支持第一部分(精度),但不支持第二部分。

使用Period 也无济于事,因为确切的时差(Period 会忽略)会以天为单位影响增量。此外(仅打印代码的输出):

    Period preciseBirthdayPeriod =
        Period.between(born.toLocalDate(), nowNormalized.toLocalDate());
    int preciseDaysOld = preciseBirthdayPeriod.getDays();
    System.out.println(preciseDaysOld); // 13
    System.out.println(preciseBirthdayPeriod); // P56Y11M13D

如您所见,使用方法preciseBirthdayPeriod.getDays() 来获取以天为单位的总增量是非常危险的。不,它只是总增量的一部分。还有 11 个月和 56 年。 我认为不仅以天为单位还打印 delta 是明智的,因为这样人们就可以更容易地想象 delta 有多大(请参阅打印持续时间的常见用例社交媒体,例如“3 年 2 个月零 4 天”)。

显然,您需要一种方法来确定持续时间,包括日历单位以及特殊时区中的时钟单位(在您的示例中:某人出生的时区)。 Java-8-time-library 的坏处是:它不支持PeriodDuration 的任何组合。并且导入外部库 Threeten-Extra-class Interval 也无济于事,因为 long daysPassed = interval.toDuration().toDays(); 仍将忽略时区影响(1 天 == 24 小时),并且也无法以其他单位(如月等)打印增量。

总结:

您已尝试过Period-解决方案。 @swiedsw 给出的答案尝试了基于Duration 的解决方案。两种方法在精度方面都有缺点。您可以尝试将这两个类合并到一个实现 TemporalAmount 的新类中,并自己实现必要的时间算术(不是那么简单)。

旁注:

我自己已经在我的时间库Time4J 中实现了您要查找的内容,因此它可能对您自己的实现有所启发。示例:

Timezone bornZone = Timezone.of(AMERICA.NEW_YORK);
Moment bornTime =
    PlainTimestamp.of(1960, net.time4j.Month.JANUARY.getValue(), 1, 22, 34, 56).in(
        bornZone
    );
Moment currentTime = Moment.nowInSystemTime();
MomentInterval interval = MomentInterval.between(bornTime, currentTime);

MachineTime<TimeUnit> mt = interval.getSimpleDuration();
System.out.println(mt); // 1797324427.356000000s [POSIX]

net.time4j.Duration<?> duration =
    interval.getNominalDuration(
        bornZone, // relevant if the moments are crossing a DST-boundary
        CalendarUnit.YEARS,
        CalendarUnit.MONTHS,
        CalendarUnit.DAYS,
        ClockUnit.HOURS,
        ClockUnit.MINUTES
    );

// P56Y11M12DT12H52M (12 days if the birth-time-of-day is after current clock time)
// If only days were specified above then the output would be: P20801D
System.out.println(duration);
System.out.println(duration.getPartialAmount(CalendarUnit.DAYS)); // 12

这个例子也表明了我的普遍态度,即使用月、日、小时等单位在严格意义上并不准确。唯一严格精确的方法(从科学的角度来看)是以十进制秒为单位使用机器时间(最好以 SI 秒为单位,也可以在 1972 年之后的 Time4J 中使用)。

【讨论】:

  • “SI-seconds”是指维基百科中定义的International Second吗?
  • @BasilBourque 是的,确切地说,国际单位制定义的 SI 秒。就给定的 Time4J 示例而言,它需要表达式 interval.getRealDuration(),但如上所述不适用于 OP 的用例,因为 OP 也使用 1960 年(1972 年之前)。
【解决方案2】:

Period 的 JavaDoc 声明它建模:

ISO-8601 日历系统中基于日期的时间量,例如“2 年 3 个月 4 天”。

我知道它没有参考时间点。

您可能想检查项目ThreeTen-Extra 中的Interval 哪些型号:

两个瞬间之间不可变的时间间隔。

项目网站声明该项目“[...] 由 Java 8 日期和时间库的主要作者 Stephen Colebourne 策划”。

您可以通过在 Interval 上调用 toDuration() 来检索 Duration

我将转换你的代码来举个例子:

ZoneId bornIn = ZoneId.of("America/New_York");
ZonedDateTime born = ZonedDateTime.of(1960, Month.JANUARY.getValue(), 1, 2, 34, 56, 0, bornIn);
ZonedDateTime now = ZonedDateTime.now();
Interval interval = Interval.of(born.toInstant(), now.toInstant());
long daysPassed = interval.toDuration().toDays();

【讨论】:

  • 好答案。换句话说,Interval 附加到时间线,而 PeriodDuration 没有。
【解决方案3】:

这两个类的主要区别是:

  • java.time.Period 使用基于日期的值(2018 年 5 月 31 日)
  • 虽然java.time.Duration 更精确,但它使用基于时间的值(“2018-05-31T11:45:20.223Z”)

【讨论】:

    【解决方案4】:

    java.time.Period 对人类阅读更友好

    • 例如 A 和 B 之间的时间段是 2 年 3 个月 3 天

    java.time.Duration 用于机器。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-04-26
      • 1970-01-01
      • 2010-11-04
      • 1970-01-01
      • 1970-01-01
      • 2020-11-22
      • 1970-01-01
      相关资源
      最近更新 更多