SimpleDateFormat does not support timezone

I have a date object in the following format:

Sun Jan 20 10:12:27 GMT + 02: 00 2013

the above time is displayed correctly in Microsoft Outlook:

Sun 1/20/2013 12:12 PM (This time is in GMT + 2 β†’)

when trying to format a date object with, it SimpleDateFormat

appears as in Outlook using the following code:

SimpleDateFormat sdf=new SimpleDateFormat(
    "EEE M/d/yyyy hh:mm a");
    String receivedDate = sdf.format(email.getDateTimeReceived());

      

formatting result:

Sun 1/20/2013 10:12 AM

so there is no two hour time zone difference.

please advise how to fix this, thanks.

+3


source to share


2 answers


If I understand correctly, you want to format the date using GMT timezone.



DateFormat dateFormat = new SimpleDateFormat("EEE M/d/yyyy hh:mm a");
dateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
String formattedDate = dateFormat.format(date);

      

+3


source


You forgot to report SimpleDateFormat

to include time zone information.

This will do the trick:

SimpleDateFormat sdf=new SimpleDateFormat(
    "EEE M/d/yyyy hh:mm a zzzZ yyyy");

      

Pay attention to the upper case Z

at the end to show the time difference

This will print:

Sun 1/20/2013 12:09 pm CET + 0100 2013



if you need it to be GMT you can force it like this:

sdf.setTimeZone(TimeZone.getTimeZone("GMT"));

      

It will now print:

Sun 1/20/2013 11:11 GMT GMT + 0000 2013

if you don't need AM / PM just remove a

+3


source







All Articles