问题:
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:
class="java" name="code">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, 转换成毫秒,就能得到正确的结果了。