Simple SimpleDateFormat analysis - one hour (using RFC 1123, GMT in summer)

I am using SimpleDateFormat with RFC 1123 to format dates and to parse dates. However, parse (format (date)) is sometimes one hour different from the original date.

Code below:

public static void main(String[] args) throws ParseException {
    String RFC1123_DATE_PATTERN = "EEE, dd MMM yyyy HH:mm:ss zzz";
    SimpleDateFormat dateFormat = new SimpleDateFormat(RFC1123_DATE_PATTERN);

    Date date = new Date(1000);
    String str = dateFormat.format(date);
    Date date2 = dateFormat.parse(str);

    System.out.println("date="+date+"; "+date.getTime());
    System.out.println("str="+str);
    System.out.println("date2="+date2+"; "+date2.getTime());
}

Writes:

date=Thu Jan 01 01:00:01 GMT 1970; 1000
str=Thu, 01 Jan 1970 01:00:01 GMT
date2=Thu Jan 01 02:00:01 GMT 1970; 3601000

I got this template from apache.http.util.DateUtil, so I expected it to work [1].

Presumably this is a confusion about whether GMT includes or excludes daylight saving time?

I am using the Java (TM) SE runtime (build 1.6.0_31-b04-415-10M3646, also tested on 1.7.0_71).


A workaround is to use the "EEE, dd MMM yyyy HH: mm: ss Z" template, which gives:

date=Thu Jan 01 01:00:01 GMT 1970; 1000
str=Thu, 01 Jan 1970 01:00:01 +0100
date2=Thu Jan 01 01:00:01 GMT 1970; 1000

[1] http://www.docjar.com/html/api/org/apache/http/util/DateUtils.java.html

: @oscar-castiblanco, new Date(1000), 1234ms. .

+3
2

"EEE, dd MMM yyyy HH: mm: ss zzz",

date=Thu Jan 01 01:00:01 CET 1970; 1234
str=Thu, 01 Jan 1970 01:00:01 CET
date2=Thu Jan 01 01:00:01 CET 1970; 1000

, .

, :

pattern = "EEE, dd MMM yyyy HH: mm: ss: SSS Z"

date=Thu Jan 01 01:00:01 CET 1970; 1234
str=Thu, 01 Jan 1970 01:00:01:234 +0100
date2=Thu Jan 01 01:00:01 CET 1970; 1234
+1

GMT . , , " ", . " " - , . , Microsoft , .

0

All Articles