Inconsistent behaviour for SimpleDateFormat for timezone Amsterdam

Yesterday I ran into a problem where the date of birth of a person was changed after it was marshalled with XStream from Date to xml and then unmarshalled to Date again. The following code reproduces the strange behaviour of XStream: System.setProperty("user.timezone", "Europe/Amsterdam"); SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S z"); String textIn = "1933-09-17 00:00:00.0 CET"; Date date = dateFormat.parse(textIn); String textOut = dateFormat.format(date); System.out.println("input : " + textIn); System.out.println("date : " + date); System.out.println("output: " + textOut); The results: input : 1933-09-17 00:00:00.0 CET date : Sun Sep 17 00:19:32 CEST 1933 output: 1933-09-17 00:19:32.0 CEST I found out that it happens only for dates before 1940. Which is in a certain way explainable: In the Netherlands in 1940 there was a change from the so called "Amsterdamse Tijd" (GMT+00h19m32s) to European time (GMT+01h00m00s). I cannot explain why the timezone changes to saving time (from CET to CEST). If I change the timezone to Berlin System.setProperty("user.timezone", "Europe/Berlin"); I get the results I expect: input : 1933-09-17 00:00:00.0 CET date : Sun Sep 17 00:00:00 CET 1933 output: 1933-09-17 00:00:00.0 CET My server is located in Amsterdam. I will set the timezone of the server to Berlin, to work around the problem. My question is: Do you think this is a bug in SimpleDateFormat? Or is the code invalid because "1933-09-17 00:00:00.0 CET" is an invalid date for location Amsterdam? If it is a bug, should and where should this be reported? If the date input is invalid in in itself, shouldn't the parse method throw an error?
Bah, dates are such a headache. I am curious to know the answer to this question.

以上就是Inconsistent behaviour for SimpleDateFormat for timezone Amsterdam的详细内容,更多请关注web前端其它相关文章!

赞(0) 打赏
未经允许不得转载:web前端首页 » JavaScript 答疑

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

前端开发相关广告投放 更专业 更精准