当前位置:首页 > 开发 > 编程语言 > Java > 正文

Java Convert Long to Date

发表于: 2014-06-16   作者:alleni123   来源:转载   浏览:
摘要: 问题: 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
问题:
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, 转换成毫秒,就能得到正确的结果了。

Java Convert Long to Date

  • 0

    开心

    开心

  • 0

    板砖

    板砖

  • 0

    感动

    感动

  • 0

    有用

    有用

  • 0

    疑问

    疑问

  • 0

    难过

    难过

  • 0

    无聊

    无聊

  • 0

    震惊

    震惊

版权所有 IT知识库 CopyRight © 2009-2015 IT知识库 IT610.com , All Rights Reserved. 京ICP备09083238号