Java:使用TimeUnit将秒转换为日,小时,分和秒
我使用TimeStamp类将秒转换为日,小时,分钟,秒。 我用下面的代码
public static void calculateTime(long seconds) { int day = (int)TimeUnit.SECONDS.toDays(seconds); long hours = TimeUnit.SECONDS.toHours(seconds) - TimeUnit.SECONDS.toHours(TimeUnit.SECONDS.toDays(seconds)); long minute = TimeUnit.SECONDS.toMinutes(seconds) - TimeUnit.SECONDS.toMinutes(TimeUnit.SECONDS.toHours(seconds)); long second = TimeUnit.SECONDS.toSeconds(seconds) - TimeUnit.SECONDS.toSeconds(TimeUnit.SECONDS.toMinutes(seconds)); System.out.println("Day " + day + " Hour " + hours + " Minute " + minute + " Seconds " + second); }
但是我没有得到正确的结果。 例如,当我把这个方法叫做calculateTime(3600)
它给了我的结果是Day 0 Hour 1 Minute 60 Seconds 3540
而不是Day 0 Hour 1 Minute 0 Seconds 0
。
我的逻辑有什么问题? 请帮帮我。
应该是这样的
int day = (int)TimeUnit.SECONDS.toDays(seconds); long hours = TimeUnit.SECONDS.toHours(seconds) - (day *24); long minute = TimeUnit.SECONDS.toMinutes(seconds) - (TimeUnit.SECONDS.toHours(seconds)* 60); long second = TimeUnit.SECONDS.toSeconds(seconds) - (TimeUnit.SECONDS.toMinutes(seconds) *60);
编辑说明:
- 日计算正确无需解释。
- TimeUnit.SECONDS.toHours(秒)将使您可以直接从秒转换为小时,而无需考虑已经计算的天数。 减去你已经有几天的时间,即一天* 24。 你现在有剩余的时间了。
- 同样的分钟和秒钟。 你需要分别减去已经得到的小时和分钟。
你可以这样做只使用TimeUnit
:
public static void calculateTime(long seconds) { int day = (int) TimeUnit.SECONDS.toDays(seconds); long hours = TimeUnit.SECONDS.toHours(seconds) - TimeUnit.DAYS.toHours(day); long minute = TimeUnit.SECONDS.toMinutes(seconds) - TimeUnit.DAYS.toMinutes(day) - TimeUnit.HOURS.toMinutes(hours); long second = TimeUnit.SECONDS.toSeconds(seconds) - TimeUnit.DAYS.toSeconds(day) - TimeUnit.HOURS.toSeconds(hours) - TimeUnit.MINUTES.toSeconds(minute); System.out.println("Day " + day + " Hour " + hours + " Minute " + minute + " Seconds " + second); }
或略短,但可能不那么直观
public static void calculateTime(long seconds) { int day = (int) TimeUnit.SECONDS.toDays(seconds); long hours = TimeUnit.SECONDS.toHours(seconds) - TimeUnit.DAYS.toHours(day); long minute = TimeUnit.SECONDS.toMinutes(seconds) - TimeUnit.HOURS.toMinutes(TimeUnit.SECONDS.toHours(seconds)); long second = TimeUnit.SECONDS.toSeconds(seconds) - TimeUnit.MINUTES.toSeconds(TimeUnit.SECONDS.toMinutes(seconds)); System.out.println("Day " + day + " Hour " + hours + " Minute " + minute + " Seconds " + second); }
简单的方法:
public static void calculateTime(long seconds) { long sec = seconds % 60; long minutes = seconds % 3600 / 60; long hours = seconds % 86400 / 3600; long days = seconds / 86400; System.out.println("Day " + days + " Hour " + hours + " Minute " + minutes + " Seconds " + sec); }
这里是我创build的代码:( 3600秒显示“天:0小时:1分钟:0秒:0”)
public class TimeConvert { public static void main(String[] args) { int fsec,d,h,m,s,temp=0,i; fsec=3600; //For Days if(fsec>=86400) { temp=fsec/86400; d=temp; for(i=1;i<=temp;i++) { fsec-=86400; } } else { d=0; } //For Hours if(fsec>=3600) { temp=fsec/3600; h=temp; for(i=1;i<=temp;i++) { fsec-=3600; } } else { h=0; } //For Minutes if(fsec>=60) { temp=fsec/60; m=temp; for(i=1;i<=temp;i++) { fsec-=60; } } else { m=0; } //For Seconds if(fsec>=1) { s=fsec; } else { s=0; } System.out.println("Days:"+d+" Hours:"+h+" Minutes:"+m+" Seconds:"+s); } }
希望它能回答你的问题。
尝试这个
public static void calculateTime(long seconds) { int day = (int)TimeUnit.SECONDS.toDays(seconds); long hours = TimeUnit.SECONDS.toHours(seconds) ; long tempSec = seconds - (TimeUnit.HOURS.toSeconds(hours) ); System.out.println("after hours calculation "+ tempSec); long minute = TimeUnit.SECONDS.toMinutes(tempSec); if(tempSec > TimeUnit.MINUTES.toSeconds(minute)){ tempSec = tempSec - (TimeUnit.MINUTES.toSeconds(minute) ); }else{ tempSec = TimeUnit.MINUTES.toSeconds(minute) - tempSec; } System.out.println("after min calculation "+ tempSec); long second = TimeUnit.SECONDS.toSeconds(tempSec) ; System.out.println("Day " + day + " Hour " + hours + " Minute " + minute + " Seconds " + second); }
public static void timeCalculator(){ Scanner input = new Scanner(System.in); System.out.print("Enter length of time in seconds: "); int n = input.nextInt(); int nDay = n/86400; int nHours = (n%86400)/3600; int nMin = ((n%86400)%3600) /60; int nSec =(((n%86400)%3600)%60); System.out.println(); System.out.print("That is "+ nDay+ " day(s),"+nHours+" hour(s), "+nMin+" minute(s), and "+nSec+" second(s). "); }