2

[Context: I'm not a developer but need to document existing code]

I understand that there are no standards for timezone abbreviations and so CST can mean Central Standard Time, China Standard Time, Cuba Standard Time, ...

But if I have code like this:

        String time = "12:00:00.000 CST Tue Dec 17 2019";
        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss.SSS zzz EEE MMM dd yyyy");
        Date utcTime = sdf.parse(time);
        System.out.println(utcTime.toGMTString());

Then the result is:

        17 Dec 2019 18:00:00 GMT

So this means that SimpleDateFormat.parse() interprets CST as Central Standard Time / UTC-6.

How/where can I get a full list of timezone abbreviations that SimpleDateFormat.parse() supports AND their meaning (i.e. just knowing that CST is supported is not enough, I need to know that this is interpreted as Central Standard Time, not Cuba Standard Time).


I would expect to find the answer on the Javadocs page for SimpleDateFormat but it only gives PST as example, not a full list.


The Javadocs page for TimeZone says :

For compatibility with JDK 1.1.x, some other three-letter time zone IDs (such as "PST", "CTT", "AST") are also supported.

But doesn't say which ones.
It also only mentions three-letter IDs, while the code above will also work fine with CEST for example (Central European Summer Time).

If I check the list returned by TimeZone.getAvailableIDs(); it does not contain CEST either, but it does contain items like "Cuba" and "Eire", which do not work in the above code snippet (even when I change zzz to zzzz in the parse string).

So I conclude that java.text.SimpleDateFormat does not support the same abbreviations as java.util.TimeZone.

Where can I find a list of abbreviations that SimpleDateFormat is able to parse?


EDIT: I have checked the question (and answers) referred to in the "closed as duplicate" message. They seem to be about TimeZone only and do not even mention SimpleDateFormat. My question is about SimpleDateFormat specifically and as mentioned above, my findings so far are that SimpleDateFormat does NOT support the same abbreviations as TimeZone, e.g.:

  • TimeZone.getAvailableIDs() includes "Cuba" but SimpleDateFormat does not parse "Cuba"
  • SimpleDateFormat does parse "CEST" but this is not includes in TimeZone.getAvailableIDs()
hertitu
  • 141
  • 4
  • While you might be able to document which zones are supported in the SimpleDateFormat implementation in the particular version of Java you’re using, the fact that it is not a standard and there is no documented mapping means the behavior cannot be reliably documented. Whatever you may discover may not be valid for later or earlier versions of Java. If I were given your task, I would simply write that 3-letter and 4-letter timezone abbreviations will result in undefined behavior. – VGR Dec 17 '19 at 15:09

0 Answers0