Java SimpleDateFormat and 19700101 conversion problem

孤街醉人 提交于 2019-12-11 23:28:20

问题


i have a little problem with date conversion in java. When i put 19700101 to SimpleDateFormat and then call getTime i got -3600000. I write test:

SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
Date date = dateFormat.parse("19700101");
System.out.println(date.getTime());
System.out.println(dateFormat.format(new Date(0)));
System.out.println((new Date(0)).getTime());

Result should be:

0
19700101
0

but i got

-3600000
19700101
0

My question is why SimpleDateFormat return -3600000 (20Nov1969)? Where I can find information about formatting and conversion bugs?


回答1:


Which time zone were you using? My guess is you were using GMT+1. Set the timezone to "GMT" and you should get the expected result.

You are using a default timezone like CEST (GMT+1). When it was 1970/01/01 00:00:00.000 CEST was 1969/12/31 23:00:00.000 GMT which is -1h from 1970/01/01 or -3600000 ms.



来源:https://stackoverflow.com/questions/6788691/java-simpledateformat-and-19700101-conversion-problem

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!