Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is SimpleDateFormat parse - Daylight Saving Time Aware?

I would like to know if the following is DST aware ie., the SimpleDateFormat parse method is DST aware if we set the Timezone of that respective country.

SimpleDateFormat sdf = new SimpleDateFormat();
sdf.setTimeZone(TimeZone.getTimeZone("CET")); //Germany
String currentDate = sdf.format((new Date()).getTime());
currentDate = sdf.parse(currentDate);

I undserstand that "CET", "MST", "EST" etc are three letter codes and is not encouraged to use this, but irrespective of this is the dateformat parse DST aware ?

Thanks in advance.

like image 980
Infinity Avatar asked Jul 10 '26 14:07

Infinity


1 Answers

In some specific cases, the three-letter codes will work - but not all of them include daylight saving time rules.

From the TZ database sources:

Europe:

# Zone  NAME    GMTOFF  RULES  FORMAT  [UNTIL]
Zone    WET     0:00    EU     WE%sT
Zone    CET     1:00    C-Eur  CE%sT
Zone    MET     1:00    C-Eur  ME%sT
Zone    EET     2:00    EU     EE%sT

North America:

# Zone  NAME    GMTOFF  RULES  FORMAT  [UNTIL]
Zone    EST      -5:00  -      EST
Zone    MST      -7:00  -      MST
Zone    HST     -10:00  -      HST

As you can see, the European file defines 4 three-letter zones for backwards compatibility purposes, all of which follow European daylight saving time rules.

However, the North American file only defines 3 of these. Notably, PST and CST are missing. Also, the EST and MST zones that are defined do not have any daylight saving time rules.

In general, you should avoid using the three-letter abbreviations. They are not all supported, the ones that are do not all support DST, and in general they can be ambiguous.

like image 50
Matt Johnson-Pint Avatar answered Jul 14 '26 02:07

Matt Johnson-Pint