There's a lot of questions on here on SimpleDateFormat but I can't seem to find anything on this issue. I'm running into issues with different output from the exact same code running on Android vs the JDK. I'm running in Eclipse and using the emulator to test Android. JDK version 1.7 and Android 4.4.
Any ideas on how to make Android output dates in the JDK style format?
TimeZone GMT_ZONE = TimeZone.getTimeZone("GMT"); String RFC1123_PATTERN = "EEE, dd MMM yyyy HH:mm:ss z"; final DateFormat rfc1123Format = new SimpleDateFormat(RFC1123_PATTERN, LOCALE_US); rfc1123Format.setTimeZone(GMT_ZONE); String dateString = rfc1123Format.format(new Date());
JDK 1.7 dateString value: Fri, 20 Dec 2013 00:46:21 GMT
Android 4.4 dateString value: Fri, 20 Dec 2013 00:46:21 GMT+00:00
Android != Java
Android libraries are an imitation of the Java libraries but not exact copies. Thus the lawsuit between Oracle and Google. So you may see variations in behavior.
If you want a consistent, and superior, experience when doing work with date-times, use the third-party open-source Joda-Time library. Joda-Time is meant to supplant the notoriously bad java.util.Date/Calendar classes.
Another option might be the Java 7 backport of JSR 310: Date and Time API java.time.* classes bundled with Java 8.
RFC 1123 Format
Regarding the follow-up question you added as a comment:
Any idea what pattern would produce the Fri, 20 Dec 2013 00:46:21 GMT format output on Android?
Surprisingly, Joda-Time 2.3 seems to lack a built-in formatter for that old RFC 1123 format. But the following home-brew format seems to do the job, provided you remember to convert the DateTime to UTC, as shown below.
// © 2013 Basil Bourque. This source code may be used freely forever by anyone taking full responsibility for doing so. // import org.joda.time.*; // import org.joda.time.format.*; DateTime nowInParis = new DateTime( DateTimeZone.forID( "Europe/Paris" ) ); DateTimeFormatter formatter = DateTimeFormat.forPattern("E, d MMM yyyy HH:mm:ss 'GMT'").withLocale( Locale.US ); String nowInParisAsStringGMT = formatter.print( nowInParis.toDateTime( DateTimeZone.UTC ) );
Dump to console…
System.out.println( "nowInParisAsStringGMT: " + nowInParisAsStringGMT ); System.out.println( "nowInParis: " + nowInParis );
nowInParisAsStringGMT: Fri, 20 Dec 2013 05:03:58 GMT nowInParis: 2013-12-20T05:03:58.175+01:00