Why can't I get a duration in minutes or hours in java.time?

后端 未结 6 2136
故里飘歌
故里飘歌 2020-11-29 02:22

Of the Duration class in the new JSR 310 date API (java.time package) available in Java 8 and later, the javadoc says :

This class models a quantity o

6条回答
  •  感动是毒
    2020-11-29 03:02

    The documentation says:

    This returns a value for each of the two supported units, SECONDS and NANOS. All other units throw an exception.

    So, best guess answer -- that's the way they designed it.

    You can use some of the other methods to get it in hours:

    long hours = duration.toHours();
    

    or minutes:

    long minutes = duration.toMinutes();
    

提交回复
热议问题