Java Convert Long to Date

问题:
I have list with long values (for example: 1220227200, 1220832000, 1221436800...) which I downloaded from web service. I must convert it to Dates. Unfortunately this way, for example:

Date d = new Date(1220227200);

returns 1 Jan 1970. Anyone know another way to convert it correctly?


回答:
The Date constructor (click the link!) accepts the time as long in milliseconds, not seconds. You need to multiply it by 1000 and make sure that you supply it as long.

Date d = new Date(1220227200L * 1000);



今天获取某网站日期的时候, 就遇到了这个问题

 String date="1402903171";


Date d=new Date((Long.parseLong(date))*1000);

SimpleDateFormat formatter=new SimpleDateFormat("yyyy-MM-dd:hh-mm");
System.out.println(formatter.format(d));


也就是说"1402903171"是秒数,如果直接转换就会出现1970年xxxx.

这里必须乘以1000, 转换成毫秒,就能得到正确的结果了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值