开发者

SimpleDateFormat give inconsistent results

开发者 https://www.devze.com 2022-12-25 09:02 出处:网络
I am trying to parse a date and I am getting different results when I run the code locally/BST compare to a server in Paris/CEST.

I am trying to parse a date and I am getting different results when I run the code locally/BST compare to a server in Paris/CEST.

I've reproduced the issue in a the following sample. This is trying to parse the start date for the Australian Grand Prix.

    TimeZone tz = TimeZone.getTimeZone("AET");
    DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy HH mm");
    dateFormat.setTimeZone(tz);
    long time = dateFormat.parse("28/03/2010 17 00").getTime();
    System.out.println("Time "+time);

It seems like I am setting the timezone correctly on the date format and the current timezone shouldn't be affecting the code. But locally it prints 1269756000000 and in Paris 1269759600000. Any idea?

Answer:

It seems like I was testing with an edge case: the T开发者_运维问答imezone definition is different on my mac compare to the linux server. If I change the timezone to be: "America/Los_Angeles" I am getting a consistent result. The linux box giving me the wrong result is running java 1.6.0-b105 which might be outdated. I'll try an upgrade


Interesting. According to the TimeZone documentation:

Three-letter time zone IDs For compatibility with JDK 1.1.x, some other three-letter time zone IDs (such as "PST", "CTT", "AST") are also supported. However, their use is deprecated because the same abbreviation is often used for multiple time zones (for example, "CST" could be U.S. "Central Standard Time" and "China Standard Time"), and the Java platform can then only recognize one of them.

It would be interesting to see the results if you use "Australia/Melbourne" instead of "AET", but just from a quick experiment that I did, it doesn't seem like it makes a difference.

It's curious that the results are an hour apart, like Daylight Savings Time isn't being taken into account in one of the cases. Stupid question; if you're running on two separate computers, are you sure the times are set correctly on each?


On my system here, the result is "1269756000000" (like on your local system). I would try to check the server in Paris, especially the settings that concern the timezone:

System.out.println(System.getProperty("user.timezone"));
System.out.println(System.getProperty("user.country"));

Maybe this brings up some differences that helps you to solve this issue.

0

精彩评论

暂无评论...
验证码 换一张
取 消