Java getHours(), getMinutes() and getSeconds()

后端 未结 3 893
离开以前
离开以前 2020-12-02 16:29

As I know getHours(), getMinutes() and getSeconds() are all deprecated in Java and they are replaced with Calendar.HOUR_OF_DAY

3条回答
  •  [愿得一人]
    2020-12-02 17:12

    For a time difference, note that the calendar starts at 01.01.1970, 01:00, not at 00:00. If you're using java.util.Date and java.text.SimpleDateFormat, you will have to compensate for 1 hour:

    long start = System.currentTimeMillis();
    long end = start + (1*3600 + 23*60 + 45) * 1000 + 678; // 1 h 23 min 45.678 s
    Date timeDiff = new Date(end - start - 3600000); // compensate for 1h in millis
    SimpleDateFormat timeFormat = new SimpleDateFormat("H:mm:ss.SSS");
    System.out.println("Duration: " + timeFormat.format(timeDiff));
    

    This will print:

    Duration: 1:23:45.678

提交回复
热议问题