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

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.

Leave a Comment

error code: 521