为什么 Duration 类没有“toSeconds()"方法? [英] Why does the Duration class not have 'toSeconds()' method?

查看:62
本文介绍了为什么 Duration 类没有“toSeconds()"方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在查看持续时间Java 8 中的类并注意到它没有:

I was looking at the Duration class in Java 8 and noticed that it does not have:

long toSeconds();

但它具有所有其他 toXXXXX() 来获取天数、小时数、分钟数、毫秒、毫微秒.我确实看到了一个 getSeconds() 方法,该方法返回此持续时间对象内的秒数.还有一个 get(TemporalUnit unit) 方法来获取持续时间作为请求的时间单位.但是为什么不保留 toSeconds() 方法以保持一致性?

But it has all other toXXXXX() to get days, hours, minutes, millis, nanos. I do see a getSeconds() method that returns the number of seconds within this duration object. There is also a get(TemporalUnit unit) method to get the duration as the requested time unit. But why not keep the toSeconds() method for consistency?

推荐答案

我们来看看文档 说:

这个类以秒和纳秒为单位对数量或时间进行建模.

This class models a quantity or amount of time in terms of seconds and nanoseconds.

这基本上意味着用于存储表示的时间量的单位是.例如,要存储 5 分 10 纳秒的持续时间,则存储 300(秒)和 10(纳秒).因此,无需将 转换为 秒.您可以使用 getSeconds()获取秒数.

That basically means that the unit used to store the amount of time represented is seconds. For example, to store the duration 5 minutes and 10 nanoseconds, 300 (seconds) and 10 (nanoseconds) are stored. Because of this, there is no need to convert to seconds. You get the seconds using getSeconds().

明白我的意思吗?所有其他方法都将转换为相应的单位:天、分钟、小时……这就是为什么它们以to开头,意思是convertedTo.由于您不需要进行转换以获取以秒为单位的持续时间,因此返回以秒为单位的持续时间的方法以 get 开头.

See what I mean here? All the other methods convert to the corresponding units: days, minutes, hours... That's why they start with to, meaning convertedTo. Since you don't need to do a conversion to get the duration in seconds, the method that returns the duration in seconds starts with get.

这篇关于为什么 Duration 类没有“toSeconds()"方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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