java.utilDate和System.currentTimeMillis()

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年毫秒值
	}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值