如何将毫秒转换为小时、分钟、秒和剩余毫秒。

huangapple go评论76阅读模式
英文:

How do I get milliseconds to hours, minutes, secs and remaining millisecs

问题

好的,以下是您提供的代码的翻译部分:

int beginning = 10000123; // beginning是起始的毫秒数
int hours = beginning / 3600000;
int rSecs = beginning % 1000; // r = 剩余
int minutes = rSecs / 60;
int seconds = rSecs % 60;
int milliSecs = rSecs % 1000;

System.out.println("Lab03, 100点版本\n");
System.out.println("起始毫秒数:" + beginning);
System.out.println("小时:" + hours);
System.out.println("分钟:" + minutes);
System.out.println("秒:" + seconds);
System.out.println("毫秒:" + milliSecs);

感谢任何帮助!

英文:

Ok, so I've recently started programming in java about a week ago, and I'm attempting to write a program that will take milliseconds and give me hours, minutes, seconds, and remaining milliseconds. Now I've got most of it down its just for some reason, giving me numbers that are off. I should be getting 2hrs, 46 min, 40 seconds, and 123 milliseconds. But instead I'm getting 2hrs, 2min, 3secs, and 123 milliseconds.

Heres my code

   int beginning = 10000123; //beginning is the starting number of milli seconds
   int hours = beginning/3600000;
   int rSecs = beginning%1000; //r = remaining
   int minutes = rSecs/60;
   int seconds = rSecs%60;
   int milliSecs = rSecs%1000;
   
   System.out.println("Lab03, 100 Point Version\n");
   System.out.println("Starting Milliseconds" + beginning); 
   System.out.println("Hours:" + hours);
   System.out.println("Minutes:" + minutes);
   System.out.println("Seconds:" + seconds);
   System.out.println("Milli Seconds:" +  milliSecs);

Thanks for any and all help!

答案1

得分: 1

你需要对 3600000 取模以获取剩余的毫秒数。

int hours = beginning / 3600000;
int rMiliSecs = beginning % 3600000;

由于这是毫秒,你需要除以 60000(1 分钟 -> 60 秒 -> 60000 毫秒)以获取分钟数,然后对 60000 取模以获取剩余的毫秒数。

int minutes = rMiliSecs / 60000;
rMiliSecs = rMiliSecs % 60000;

然后除以 1000 以获取秒数,并对 1000 取模(1 秒 -> 1000 毫秒)以获取剩余的毫秒数。

int seconds = rMiliSecs / 1000;
int milliSecs = rMiliSecs % 1000;
英文:

You need to modulo by 3600000 for remaining milli second

int hours = beginning/3600000;
int rMiliSecs = beginning%3600000;

Since it millisecond you need to divide by 60000(1m-> 60s -> 60000ms) for minute and modulo by 60000 to get remainings

int minutes = rMiliSecs/60000;
rMiliSecs = rMiliSecs%60000;

Then divide by 1000 for the second and modulo by 1000(1s -> 1000ms) for remaining millisecond

int seconds = rMiliSecs/1000;
int milliSecs = rMiliSecs%1000;

答案2

得分: 0

如果您想避免使用神奇数字并采用更面向对象的方法,您可以使用 Duration 类。

Duration duration = Duration.ofMillis(10000123);
long hours = duration.toHours();
duration = duration.minusHours(hours);
long minutes = duration.toMinutes();
duration = duration.minusMinutes(minutes);
long seconds = duration.getSeconds();
duration = duration.minusSeconds(seconds);
long milliSec = duration.toMillis();

英文:

If you want to avoid the magic numbers and do a more OO approach you can use the Duration class.

                Duration duration = Duration.ofMillis(10000123);
                long hours = duration.toHours();
                duration = duration.minusHours(hours);
                long minutes = duration.toMinutes();
                duration = duration.minusMinutes(minutes);
                long seconds = duration.getSeconds();
                duration = duration.minusSeconds(seconds);
                long milliSec = duration.toMillis();

huangapple
  • 本文由 发表于 2020年9月3日 23:34:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/63727020.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定