How to convert Milliseconds to “X mins, x seconds” in Java?

后端 未结 27 2187
夕颜
夕颜 2020-11-22 03:59

I want to record the time using System.currentTimeMillis() when a user begins something in my program. When he finishes, I will subtract the current Syste

27条回答
  •  天涯浪人
    2020-11-22 04:28

    Using the java.time package in Java 8:

    Instant start = Instant.now();
    Thread.sleep(63553);
    Instant end = Instant.now();
    System.out.println(Duration.between(start, end));
    

    Output is in ISO 8601 Duration format: PT1M3.553S (1 minute and 3.553 seconds).

提交回复
热议问题