java - Java Period 和 Duration 之间的微妙之处

标签 java time duration

我不确定我是否理解了 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();

但是要得到一个精确的答案似乎真的很复杂。

最佳答案

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

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

通常情况下,在计算一个人的年龄时忽略时钟精度或时区就足够了,因为护照等个人文件不会记录某人出生的确切时间。如果是这样,则 Period 类完成其工作(但请小心处理其方法,如 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 年。我认为不仅以天为单位打印增量是明智的,因为这样人们就可以更容易地想象增量有多大(请参阅打印持续时间的常见用例社交媒体,例如“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 中)。

关于java - Java Period 和 Duration 之间的微妙之处,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41123166/

相关文章:

r - 转换字符 (HH :MM) to Minutes

jquery - 动画 jQuery 函数

google-sheets - 如何格式化 Google Sheets 持续时间格式的单元格以以人类可读的格式显示天数、小时数和分钟数?

java - 小程序中未找到主要方法错误

java - 使用 Selenium Webdriver 查找并切换到正确的框架

java - 凯撒密码移位问题

swift - 在 Swift 3 中获取时差

java - 与 Python 不同,Java 中的 return 语句可以执行多次吗?

c++ - 使用派生单位的计算

java - 使用 Joda 时间求和持续时间