import java.util.Scanner;
public class Timel {
private static Scanner in;
public static void main(String[] args) {//实现当前系统时间戳的转换,输入一个有效的时间进行转换
System.out.println("时光机系统开启,当前北京时间:");
TimeCp now=new TimeCp(System.currentTimeMillis()/1000);
System.out.println(now.cpTime());
System.out.println("请输入想去的时间点距离(1970为起点):");
in = new Scanner(System.in);
long tp=in.nextLong();
TimeCp some=new TimeCp(tp);
System.out.println(some.cpTime());
System.out.print("你穿越到");
System.out.println(some.cpTime()+"是否查看其他几区时间yes->1");
int a=in.nextInt();
if(a==1)
{
System.out.println(some.timeArea(some.cpTime()));
}
}
}
class TimeCp
{
private long time;
public TimeCp(long x)
{
time=x;
}
public long[] cpTime()//构造器,将时间戳转换为年月日...格式
{
long year=1970,month=1,day=1,hour=0,minute=0,second=0;
second=time%60;//取模是算法中非常有用的一个技巧,从奇偶数的判别到素数的判别,从模幂运算到最大公约数的求法,猴子选王(抽象的讲它可以实现循环)
minute=time%3600/60;//不足一小时的秒数作为分钟
hour=(time+86400)%86400/3600+8;
day=time/86400+1;
while(day>=32)//因为每个月的天数没有太大规律(有发现的朋友可以优化),所以用if进行年的循环。主要思想就是天数减完,年加,不足就退出,还有就是闰年的问题
{
day=day-31;
month++;
if(((year%4)==0&&(year%100)!=0)||(year%400==0))
{
if(day>=30)
{
day=day-29;
month++;
}
}
else
{
if(day>=29)
{
day=day-28;
month++;
}
}
if(day>=32)
{
day=day-31;
month++;
}
if(day>=31)
{
day=day-30;
month++;
}
if(day>=32)
{
day=day-31;
month++;
}
if(day>=31)
{
day=day-30;
month++;
}
if(day>=32)
{
day=day-31;
month++;
}
if(day>=32)
{
day=day-31;
month++;
}
if(day>=31)
{
day=day-30;
month++;
}
if(day>=32)
{
day=day-31;
month++;
}
if(day>=31)
{
day=day-30;
month++;
}
if(day>=32)
{
day=day-31;
month++;
month=1;
year++;
}
}
System.out.println(year+"-"+month+"-"+day+" "+hour+":"+minute+":"+second);
long[] tmp={year,month,day,hour,minute,second};
return tmp;
}
public long timeArea(long[] x)//时区转换
{
System.out.println("七区"+x[0]+"-"+x[1]+"-"+x[2]+" "+(x[3]-1)+":"+x[4]+":"+x[5]);
System.out.println("六区"+x[0]+"-"+x[1]+"-"+x[2]+" "+(x[3]-2)+":"+x[4]+":"+x[5]);
System.out.println("五区"+x[0]+"-"+x[1]+"-"+x[2]+" "+(x[3]-3)+":"+x[4]+":"+x[5]);
System.out.println("四区"+x[0]+"-"+x[1]+"-"+x[2]+" "+(x[3]-4)+":"+x[4]+":"+x[5]);
System.out.println("三区"+x[0]+"-"+x[1]+"-"+x[2]+" "+(x[3]-5)+":"+x[4]+":"+x[5]);
System.out.println("二区"+x[0]+"-"+x[1]+"-"+x[2]+" "+(x[3]-6)+":"+x[4]+":"+x[5]);
System.out.println("一区"+x[0]+"-"+x[1]+"-"+x[2]+" "+(x[3]-7)+":"+x[4]+":"+x[5]);
System.out.println("本区"+x[0]+"-"+x[1]+"-"+x[2]+" "+(x[3]-8)+":"+x[4]+":"+x[5]);
return 0;
}