From milliseconds to hour, minutes, seconds and milliseconds

前端 未结 9 1850
失恋的感觉
失恋的感觉 2020-12-07 21:22

I need to go from milliseconds to a tuple of (hour, minutes, seconds, milliseconds) representing the same amount of time. E.g.:

10799999ms = 2h 59m 59s 999ms

9条回答
  •  一向
    一向 (楼主)
    2020-12-07 21:46

    Just an other java example:

    long dayLength = 1000 * 60 * 60 * 24;
    long dayMs = System.currentTimeMillis() % dayLength;
    double percentOfDay = (double) dayMs / dayLength;
    int hour = (int) (percentOfDay * 24);
    int minute = (int) (percentOfDay * 24 * 60) % 60;
    int second = (int) (percentOfDay * 24 * 60 * 60) % 60;
    

    an advantage is that you can simulate shorter days, if you adjust dayLength

提交回复
热议问题