//计算自1970年1月1日 ,根据函数System.currentTimeMillis()返回的毫秒数得到日期
class ch0533 {
public static void main(String[] args) {
long ms=System.currentTimeMillis();
System.out.println(formatTime(ms));
}
public static String formatTime(long ms) {
int month_days[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int month_days_leap[] = {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; //闰年的月数
int h1=1000*60*60*8;//东八区所以要加八个小时
ms+=h1;
int ss = 1000;
int mi = ss * 60;
int hh = mi * 60;
int dd = hh * 24;
int mm=1;//一月
int yy=1970;
long day = ms / dd;
long d2=ms/dd;
d2++;//一号开始算
for(;;)
{
if(isleapyear(yy))
{ if(d2<366)
break;
else {yy++;
d2-=366;}
}
else {
if(d2<365)
break;
else {yy++;
d2-=365;}
}
}
for(int i=0;i<12;i++)
{
if(isleapyear(yy))
{ if(d2<month_days_leap[i])
{mm=i+1;break;}
else
d2-=month_days_leap[i];}
else
{ if(d2<month_days[i])
{mm=i+1;break;}
else
d2-=month_days[i];}
}
long hour = (ms - day * dd) / hh;
long minute = (ms - day * dd - hour * hh) / mi;
long second = (ms - day * dd - hour * hh - minute * mi) / ss;
long milliSecond = ms - day * dd - hour * hh - minute * mi - second * ss;
String strHour = hour < 10 ? "0" + hour : "" + hour;
String strMinute = minute < 10 ? "0" + minute : "" + minute;
String strSecond = second < 10 ? "0" + second : "" + second;
String strMilliSecond = milliSecond < 10 ? "0" + milliSecond : "" + milliSecond;//毫秒
strMilliSecond = milliSecond < 100 ? "0" + strMilliSecond : "" + strMilliSecond;
return yy+"年"+mm+"月"+d2+"日"+strHour + " 小时 "+strMinute + " 分钟 " + strSecond + " 秒";}
public static boolean isleapyear(int yy)
{
if( (yy % 4 == 0 && yy % 100 != 0) || yy % 400 == 0)
return true;
else
return false;
}
}
Get the date according to the number of milliseconds returned by the function System.currentTimeMillis()
Guess you like
Origin blog.csdn.net/qq_32907195/article/details/112792408
Ranking