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

后端 未结 27 2162
夕颜
夕颜 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:23

        long startTime = System.currentTimeMillis();
        // do your work...
        long endTime=System.currentTimeMillis();
        long diff=endTime-startTime;       
        long hours=TimeUnit.MILLISECONDS.toHours(diff);
        diff=diff-(hours*60*60*1000);
        long min=TimeUnit.MILLISECONDS.toMinutes(diff);
        diff=diff-(min*60*1000);
        long seconds=TimeUnit.MILLISECONDS.toSeconds(diff);
        //hour, min and seconds variables contains the time elapsed on your work
    

提交回复
热议问题