Ali*_*Ali 47 java api-design java-8
我正在查看Java 8 中的Duration类,并注意到它没有:
long toSeconds();
Run Code Online (Sandbox Code Playgroud)
但它有其他所有toXXXXX()可以获得天,小时,分钟,毫秒,纳米.我确实看到一个getSeconds()方法返回此持续时间对象中的秒数.还有一种get(TemporalUnit unit)方法可以将持续时间作为请求的时间单位.但为什么不保持toSeconds()方法的一致性?
Swe*_*per 57
让我们来看看文档说的内容:
此类以秒和纳秒为单位模拟时间量或时间量.
这基本上意味着用于存储所表示的时间量的单位是秒.例如,存储持续时间为5分钟和10纳秒,存储300(秒)和10(纳秒).因此,无需转换为秒.你得到了秒getSeconds().
看看我的意思?所有其他方法转换为相应的单位:天,分钟,小时......这就是他们开始的原因to,意思convertedTo.由于您不需要进行转换以获得持续时间(以秒为单位),因此以秒为单位返回持续时间的方法将以get.
JB *_*zet 40
这是一个已知问题,其修复程序安排在Java 9中:https://bugs.openjdk.java.net/browse/JDK-8142936
Java 9中添加的新方法toSeconds.请参阅源代码.
/**
* Gets the number of seconds in this duration.
* <p>
* This returns the total number of whole seconds in the duration.
* <p>
* This instance is immutable and unaffected by this method call.
*
* @return the whole seconds part of the length of the duration, positive or negative
*/
public long toSeconds() {
return seconds;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4160 次 |
| 最近记录: |