Duration#toDays 和 Duration#toDaysPart 是多余的吗?

Is Duration#toDays and Duration#toDaysPart redundant?

在 Java 8 中,Duration class offered the toDays 方法,return将总天数作为与日历日无关的 24 小时时间块的计数。

在 Java 9 中,Duration class 得到了方便的 to…Part 方法:toDaysParttoHoursParttoMinutesPart , toSecondsPart, toMillisPart, toNanosPart。我了解小时、分钟等的需要。但我想知道 toDaysPart

我的问题是:

Duration#toDays and Duration#toDaysPart 是否会 return 特定 Duration 对象的不同值?

在Java11中,OpenJDK中的这几行源代码一模一样.

Duration#toDays:

public long toDays() {
    return seconds / SECONDS_PER_DAY;
}

Duration#toDaysPart

public long toDaysPart(){
    return seconds / SECONDS_PER_DAY;
}

As of Java 16,没有说明哪些已被弃用,哪些未被弃用。所以...保持警惕,这是我能给你的最好建议。

get(TemporalUnit) 继承自 TemporalAmount(以及 getSeconds() 和奇怪的名字 getNano())的目的是支持提取时间分量组成的价值。 Duration 实际上只支持 SECONDSNANOS 组件访问,因为 Duration 只是 long 秒组件和 int 纳秒组件的组合,它们一起提供了纳秒分辨率 表示的定向值,幅度高达 2^63 秒(正负)。 get_ 访问器 return 只是 命名组件,而 to_ 方法 return 复合整体值下限到目标单元。最后,如果 Duration 被想象为由每个 ChronoUnit 值的一个实例组成,则 Duration 上的 to_Part 方法模拟 get_ 方法的行为。此虚拟组件访问中的奇数是 toDaysPart。我怀疑这样做的原因是,虽然 HOURS、MINUTES、SECONDS、MILLIS、MICROS 和 NANOS 在它们引用的组件方面可以被视为离散且不重叠,但在将 DAYS 视为组件值时,不清楚我们的意思是什么星期几、一个月中的某一天或一年中的某一天——这三个都是常见的约定。同样,MONTHS 和 YEARS 的长度也是可变的。因此,作者似乎选择了明确并考虑使用 DAYS 来表示该层次结构中最粗糙的组件。我个人认为这有点像狗的早餐,我想知道为什么这两种方法在得出这个结论之前有一段时间是相同的。

这些方法不只是做同样的事情;他们指定在文档中做同样的事情(在你的问题中链接):

public long toDays()

Gets the number of days in this duration. This returns the total number of days in the duration by dividing the number of seconds by 86400. This is based on the standard definition of a day as 24 hours. This instance is immutable and unaffected by this method call.

Returns: the number of days in the duration, may be negative

public long toDaysPart()

Extracts the number of days in the duration. This returns the total number of days in the duration by dividing the number of seconds by 86400. This is based on the standard definition of a day as 24 hours. This instance is immutable and unaffected by this method call.

Returns: the number of days in the duration, may be negative

唯一的区别是描述性摘要中的“获取”与“提取”一词。这些词在此上下文中没有不同的含义,其余词逐字相同,特别是指定方法 return 相同的部分。事实上,toDaysPart 的 (OpenJDK) 文档是 recently changed 以阐明这些方法做同样的事情。所以是的,它们是多余的。

根据问题跟踪器上的 the relevant issue,所有 to...Part 方法都被添加在一起,并且没有对 toDaysPart 是多余的这一事实发表任何评论;所以我们只能推测添加冗余方法的基本原理。