Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8142936

Add java.time.Duration methods for days, hours, minutes, seconds, etc.

    Details

    • Type: Enhancement
    • Status: Resolved
    • Priority: P4
    • Resolution: Fixed
    • Affects Version/s: 9
    • Fix Version/s: 9
    • Component/s: core-libs
    • Labels:
    • Subcomponent:
    • Resolved In Build:
      b97

      Description

      The Duration class has no methods to obtain the broken down parts of the duration. This has frustrated users, thus it seems wise to add methods to do this:

      - toNanosPart()
      - toMillisPart()
      - toSecondsPart()
      - toMinutesPart()
      - toHoursPart()
      - toDaysPart()

      These would return the breakdown of the duration that is impossible to
      access without user-written maths at present.

      Given a duration of 49H30M20.123S
      - toNanosPart() = 123000000
      - toMillisPart() = 123
      - toSecondsPart() = 20
      - toMinutesPart() = 30
      - toHoursPart() = 1
      - toDaysPart() = 2

      In addition, users have found the lack of a `toSeconds()` method confusing. The method exists, but is named `getSeconds()`, whereas all the similar methods are named `toXxx`. Adding a convenience method here would make the API more usable. Note that this will require renaming an existing private method.

        Issue Links

          Activity

          Hide
          ntv Nadeesh Tv (Inactive) added a comment - - edited
          Hi Stephen,
          Since we have both getNano() and toNanos() replacing getSeconds() with toSeconds(), won't it create more confusion?
          Show
          ntv Nadeesh Tv (Inactive) added a comment - - edited Hi Stephen, Since we have both getNano() and toNanos() replacing getSeconds() with toSeconds(), won't it create more confusion?
          Hide
          scolebourne Stephen Colebourne added a comment -
          Overall, it makes it more consistent:

          The toXxx() methods will return the total number of the unit, ignoring smaller units
          The toXxxPart() methods will return the relevant "part" of the duration
          The getXxx() methods will return the internal state of the object, only second and nano

          Note that getNano() and toNanos() are quite different. It just happens that getSecond() and toSecond() end up returning the same thing. Users accessing the internal state will use getXxx(). Users accessing the data who don't care about implementation details will use toXxx() and toXxxPart().
          Show
          scolebourne Stephen Colebourne added a comment - Overall, it makes it more consistent: The toXxx() methods will return the total number of the unit, ignoring smaller units The toXxxPart() methods will return the relevant "part" of the duration The getXxx() methods will return the internal state of the object, only second and nano Note that getNano() and toNanos() are quite different. It just happens that getSecond() and toSecond() end up returning the same thing. Users accessing the internal state will use getXxx(). Users accessing the data who don't care about implementation details will use toXxx() and toXxxPart().
          Hide
          ntv Nadeesh Tv (Inactive) added a comment -
          Ok. I misunderstood that we are going to replace getSecond() by toSecond(). Now I understood it.

          1 more doubt.
          Does the method declaration should look like following?
          long toNanosPart(String duration);
          int toHoursPart(String duration);
          Show
          ntv Nadeesh Tv (Inactive) added a comment - Ok. I misunderstood that we are going to replace getSecond() by toSecond(). Now I understood it. 1 more doubt. Does the method declaration should look like following? long toNanosPart(String duration); int toHoursPart(String duration);
          Hide
          scolebourne Stephen Colebourne added a comment -
          No, the method looks like this:

          int toNanosPart();
          int toHoursPart();

          and so on.

          toNanosPart() returns (nano)
          toMillisPart() returns (nano / 1,000,000)
          toSecondsPart() returns (second % 60)
          toMinutesPart() returns ((second / 60) % 60)
          toHoursPart() returns ((second / 3600) % 24)
          toDaysPart() returns (second / 86400)

          NOTE: these algorithms are psuedo code and probably incorrect for negative durations!
          Show
          scolebourne Stephen Colebourne added a comment - No, the method looks like this: int toNanosPart(); int toHoursPart(); and so on. toNanosPart() returns (nano) toMillisPart() returns (nano / 1,000,000) toSecondsPart() returns (second % 60) toMinutesPart() returns ((second / 60) % 60) toHoursPart() returns ((second / 3600) % 24) toDaysPart() returns (second / 86400) NOTE: these algorithms are psuedo code and probably incorrect for negative durations!
          Show
          ntv Nadeesh Tv (Inactive) added a comment - http://mail.openjdk.java.net/pipermail/core-libs-dev/2015-November/036961.html
          Hide
          hgupdate HG Updates added a comment -
          URL: http://hg.openjdk.java.net/jdk9/dev/jdk/rev/7f644a5d554a
          User: rriggs
          Date: 2015-12-09 20:53:14 +0000
          Show
          hgupdate HG Updates added a comment - URL: http://hg.openjdk.java.net/jdk9/dev/jdk/rev/7f644a5d554a User: rriggs Date: 2015-12-09 20:53:14 +0000
          Hide
          hgupdate HG Updates added a comment -
          URL: http://hg.openjdk.java.net/jdk9/jdk9/jdk/rev/7f644a5d554a
          User: lana
          Date: 2015-12-16 19:10:55 +0000
          Show
          hgupdate HG Updates added a comment - URL: http://hg.openjdk.java.net/jdk9/jdk9/jdk/rev/7f644a5d554a User: lana Date: 2015-12-16 19:10:55 +0000

            People

            • Assignee:
              ntv Nadeesh Tv (Inactive)
              Reporter:
              scolebourne Stephen Colebourne
            • Votes:
              0 Vote for this issue
              Watchers:
              3 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: