System.currentTimeMillis():
获取的是距1970年1月1日的毫秒值,通过/1000/3600%24(%24取余数,就是最后一天的多少小时)计算+8才能得到东八区北京时间;
new Date(int year,int month,int date):
year是减去1990年的,getYear()方法也是日期表示的年份减去 1900。
public static void main(String[] args) {
long s = System.currentTimeMillis(); //毫秒/1000=秒
long currentHours = s/1000/3600%24+8;//返回的GMT、UTC(比GMT更精确)时间,北京时间东八区
System.out.println(s+":时间戳转化为:"+currentHours);
System.out.println("===============");
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd HH:mm:ss.SSS");
//new Date()是和操作系统对应,东八区的时间
String now = sdf.format(new Date());
System.out.println("now : "+now); //20200425 23:23:22.386
System.out.println(sdf.format(s));//20200425 23:23:22.329 s是在上面获取的,早一点
System.out.println("日子为:"+date.getDate()); //返回日 1-31之间
System.out.println("隔1900年有:"+date.getYear());//1900年到现在历经的年 2020-1900=120
//120年,120*365*24=1051200 1051200+(3*30+25)*24=1053960-->2020.4.25距离1900.1.1
System.out.println("隔1970年有n年(/会自己取整化为int)"+s/1000/3600/24/365);//50年 返回的是隔1970年毫秒值
}