Search code examples
javajava-8java-9java-timeduration

Is Duration#toDays and Duration#toDaysPart redundant?


In Java 8, the Duration class offered the toDays method, returning a total number of days as a count of 24-hour chunks of time unrelated to calendar days.

In Java 9, the Duration class gained handy to…Part methods: toDaysPart, toHoursPart, toMinutesPart, toSecondsPart, toMillisPart, toNanosPart. I understand the need for the hours, minutes, etc. But I wonder about toDaysPart.

My question is:

➥ Will Duration#toDays and Duration#toDaysPart ever return different values for a particular Duration object?


Solution

  • In Java 11, these lines of source code in OpenJDK are exactly the same.

    Duration#toDays:

    public long toDays() {
        return seconds / SECONDS_PER_DAY;
    }
    

    Duration#toDaysPart

    public long toDaysPart(){
        return seconds / SECONDS_PER_DAY;
    }
    

    As of Java 16, no indication is made as to which is deprecated or which is not. So...keep your eyes peeled for it, is the best advice I could give you here.