I am getting date string from SAX parsing like this: Wed, 18 Apr 2012 07:55:29 +0000
Now, I want this string as : Apr 18, 2012 01:25 PM
The modern answer to this question is overdue. When this question was asked in 2012, using SimpleDateFormat and/or DateFormat as the other answers do, was right even though those classes had always been troublesome. They were replaced just a couple of years later by java.time, the modern Java date and time API, which I frankly find much nicer to work with. So so I am doing.
I suggest you separate your conversion into two operations. In you program don’t keep date and time as a string. Keep them as a proper date-time object. So when parsing from SAX also parse the date-time string into an OffsetDateTime immediately. When at a later point you need to show the date and time to the user (or transmit it to another system), convert it into the user’s time zone and format it into an appropriate string for that purpose.
Parse into an OffsetDateTime
The string you got conforms with RFC 1123 format. java.time has a built-in formatter for that, which is good because it saves us from constructing our own formatter.
String stringFromSaxParsing = "Wed, 18 Apr 2012 07:55:29 +0000";
OffsetDateTime dateTime = OffsetDateTime.parse(
stringFromSaxParsing, DateTimeFormatter.RFC_1123_DATE_TIME);
System.out.println(dateTime);
Output from this snippet is:
2012-04-18T07:55:29Z
Convert to user’s time zone and format
DateTimeFormatter wantedFormatter = DateTimeFormatter.ofPattern("MMM dd, uuuu hh:mm a", Locale.ENGLISH);
String formatted = dateTime.atZoneSameInstant(ZoneId.systemDefault())
.format(wantedFormatter);
System.out.println(formatted);
I ran this in Asia/Colombo time zone and got the desired:
Apr 18, 2012 01:25 PM
java.time works nicely on both older and newer Android devices. It just requires at least Java 6.
org.threeten.bp with subpackages.java.time was first described.java.time to Java 6 and 7 (ThreeTen for JSR-310).