Java Convert Long to Date_JAVA_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > JAVA > Java Convert Long to Date

Java Convert Long to Date

 2014/6/16 19:09:48  alleni123  程序员俱乐部  我要评论(0)
  • 摘要:问题:Ihavelistwithlongvalues(forexample:1220227200,1220832000,1221436800...)whichIdownloadedfromwebservice.ImustconvertittoDates.Unfortunatelythisway,forexample:Dated=newDate(1220227200);returns1Jan1970.Anyoneknowanotherwaytoconvertitcorrectly?回答
  • 标签:Java
问题:
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, 转换成毫秒,就能得到正确的结果了。
上一篇: Line用户原创表情贴图上线1个月 创收百万美元 下一篇: 没有下一篇了!
  • 相关文章
发表评论
用户名: 匿名