转换为long to date时获取意外结果日期

时间:2013-05-01 14:12:15

标签: android

我有这个时间:1365443145000

我需要获得合理的约会(dd/MM/yyyy)

我尝试:Date d = new Date(1365443145000L * 1000);

但我得到了:45239-03-04

而不是正确的结果是:08/04/2013 20:45

1 个答案:

答案 0 :(得分:2)

当你以毫秒为单位得到时间时,不要乘以1000。您可以使用以下代码将日期(以毫秒为单位)转换为日期,一个使用Date构造函数,另一个使用Calendar

像这样称呼它

Date d=new Date(1365443145000L);
    System.out.println(d.toString());

<强>输出: Mon Apr 08 17:45:45 UTC 2013

检查此链接

http://www.browxy.com/SubmittedCode/17928

修改

要转换所需格式,请使用此代码

 Calendar caldate=Calendar.getInstance();
    caldate.setTimeInMillis(1365443145000L);
    SimpleDateFormat sdf=new SimpleDateFormat("dd/MM/yyyy hh:mm");
    String strdate=sdf.format(caldate.getTime());
    System.out.println(strdate);