java 毫秒转分钟和秒,将毫秒转换为分钟和秒?

迷南。 2023-01-24 02:19 153阅读 0赞

![Image 1][]

I have looked through previous questions, but none had the answer I was looking for.

How do I convert milliseconds from a StopWatch method to Minutes and Seconds?

I have:

watch.start();

to start the stopwatch and

watch.stop();

to stop the watch. I later have

watch.getTime();

which returns Milliseconds. I want it to return in Seconds and Minutes. How do I go about doing so? I’m looking for a way to do it without multiplying/dividing by 1000 but rather a method that will make the whole computation more readable and less error-prone.

解决方案

I would suggest using TimeUnit. You can use it like this:

long minutes = TimeUnit.MILLISECONDS.toMinutes(millis);

long seconds = TimeUnit.MILLISECONDS.toSeconds(millis);

[Image 1]:

发表评论

表情:
评论列表 (有 0 条评论,153人围观)

还没有评论,来说两句吧...

相关阅读