为什么 Duration 类没有“toSeconds()"方法?

编程入门 行业动态 更新时间:2024-10-07 12:19:41
本文介绍了为什么 Duration 类没有“toSeconds()"方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我正在查看持续时间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屋!

更多推荐

[db:关键词]

本文发布于:2023-04-18 03:24:47,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/928308.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:方法   Duration   toSeconds   quot

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!