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

后端 未结 6 2134
故里飘歌
故里飘歌 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:10

    To get the hour/minute/second components in a "normalised" way, you need to calculate them manually - the code below is essentially copied from the Duration#toString method:

    Duration duration = Duration.ofSeconds(3000);
    long hours = duration.toHours();
    int minutes = (int) ((duration.getSeconds() % (60 * 60)) / 60);
    int seconds = (int) (duration.getSeconds() % 60);
    System.out.println(hours + ":" + minutes + ":" + seconds);
    

提交回复
热议问题