简体   繁体   English

将Java Date转换为Epoch会得出错误的结果

[英]Converting Java Date to Epoch gives wrong result

I am converting java Date object to epoch using code : 我正在使用代码将java Date对象转换为纪元:

        String str = "" + date;
        SimpleDateFormat df = new SimpleDateFormat("yyyy-mm-dd HH:mm:ss.SSS");
        Date formateDate = df.parse(str);
        long epoch = formateDate.getTime();
        return epoch;

If I test this with value 2013-04-26 08:34:55.705 then it gives Long as 1359189295705 which is actually Sat, 26 Jan 2013 08:34:55 GMT but its Friday today why does it say that its Saturday on 26th January 2013. 如果我使用值2013-04-26 08:34:55.705进行测试,那么它给出的Long就是1359189295705 ,实际上是Sat, 26 Jan 2013 08:34:55 GMT但今天是星期五,为什么它说它是1月26日的星期六2013。

It's using January not April . 它使用的是一月而不是四月 January 26th was indeed a Saturday. 1月26日确实是星期六。 It's given you the wrong month because your date format is wrong: 给您错误的月份,因为您的日期格式不正确:

yyyy-mm-dd HH:mm:ss.SSS

You're using mm for the month, when you should be using MM . 您使用mm为一个月,你应该使用MM The date format should be: 日期格式应为:

yyyy-MM-dd HH:mm:ss.SSS

Your format didn't specify the month anywhere, so presumably it just defaulted to January. 您的格式没有在任何地方指定月份,因此大概只是默认为1月。

month的格式不正确,应为yyyy-MM-dd ,因此默认为您的第一个月。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM