# Java：使用TimeUnit将秒转换为日，小时，分和秒

``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); }` `

` ` 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);` `

1. 日计算正确无需解释。
2. TimeUnit.SECONDS.toHours（秒）将使您可以直接从秒转换为小时，而无需考虑已经计算的天数。 减去你已经有几天的时间，即一天* 24。 你现在有剩余的时间了。
3. 同样的分钟和秒钟。 你需要分别减去已经得到的小时和分钟。

` `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); }` `

` `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). "); }` `