解析:
方法一:
import java.util.Scanner;
public class ExaminationF {
public static int[] month = {31,28,31,30,31,30,31,31,30,31,30,31};
public static boolean isLeapYear;
public static boolean isLeapYear(int year){
if ((year%4==0&&year%100!=0)||(year%400==0)) {
return true;
}
return false;
}
public static long getOffYearsMillis(long timeMillis){
long yearMillis = 0;
long sumMillis = 0;
for (int year = 1970; sumMillis <= timeMillis ; year++) {
//☆☆☆☆☆long型数据的计算必须加上l,否则可能导致超出范围
int day = isLeapYear(year)==true?366:365;
isLeapYear = isLeapYear(year);
yearMillis = day*24*60*60*1000l;
sumMillis += yearMillis;
}
return timeMillis - (sumMillis - yearMillis);
}
public static long getOffMonthMills(long offYearsMillis){
long monthMillis = 0;
long sumMillis = 0;
if (isLeapYear==true) {
month[1] = 29;
}
for (int i = 0; i < 12 && sumMillis <= offYearsMillis; i++) {
//☆☆☆☆☆long型数据的计算必须加上l,否则可能导致超出范围
monthMillis = month[i]*24*60*60*1000l;
sumMillis += monthMillis;
}
return offYearsMillis - (sumMillis-monthMillis);
}
public static long getOffDayMillis(long offMonthMills){
long dayMillis = 0;
long sumMillis = 0;
for (int i = 0; i < 31 && sumMillis <= offMonthMills; i++) {
//☆☆☆☆☆long型数据的计算必须加上l,否则可能导致超出范围
dayMillis = 24*60*60*1000l;
sumMillis += dayMillis;
}
return offMonthMills - (sumMillis-dayMillis);
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
long timeMillis = scanner.nextLong();
long offYearsMillis = getOffYearsMillis(timeMillis);
long offMonthMills = getOffMonthMills(offYearsMillis);
long offDayMillis = getOffDayMillis(offMonthMills);
long hour = offDayMillis/(60*60*1000)%24;
long minus = offDayMillis/(60*1000)%60;
long second = offDayMillis/(1000)%60;
//System.out.println(String.format("hour=%d,minus=%d,second=%d",hour,minus,second));
System.out.println(String.format("%02d:%02d:%02d",hour,minus,second));
}
}
方法二:
public class Main
{
public static void main(String[] args) throws ParseException {
Scanner scan=new Scanner(System.in);
long Ms=scan.nextLong();//接收整数时间——毫秒,1ms=0.001s
long S=Ms/1000;//得到有多少秒
long Minute=S/60;
long Hour=Minute/60;
String Date=String.format("%02d",Hour%24)+":"+String.format("%02d",Minute%60)+":"+String.format("%02d",S%60);
System.out.println(Date);
}
}
参考材料