Why does the Duration class not have 'toSeconds()' method?

Ali :

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

long 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?

Sweeper :

Let's look at what the docs say:

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

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().

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.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=424360&siteId=1