6

In Java, How to parse "Wed, 05 Jun 2013 00:48:12 GMT" into a Date object, then print the date object out in the same format.

I have tried this:

String dStr= "Wed, 05 Jun 2013 00:48:12 GMT"; 
SimpleDateFormat ft =  new SimpleDateFormat ("E, dd MMM yyyy hh:mm:ss ZZZ"); 
Date t = ft.parse(dStr); 
System.out.println(t); 
System.out.println(ft.format(t));

The result is

Wed Jun 05 10:48:12 EST 2013
Wed, 05 Jun 2013 10:48:12 +1000 

Thanks in advance:

4
  • Does this really produce a 10 hour difference, or is that piece of text bad copied? Commented Aug 13, 2013 at 22:52
  • @MartijnCourteaux No, it looks like it simply outputs in the local timezone. Commented Aug 13, 2013 at 22:53
  • Ah, now I see :) I was confusing with +100 instead of +1000. Which would have still be an hour... Commented Aug 13, 2013 at 22:57
  • Here: System.out.println("Wed, 05 Jun 2013 00:48:12 GMT"); Commented Aug 13, 2013 at 23:00

4 Answers 4

4

You don't have an error, both: Wed, 05 Jun 2013 00:48:12 GMT and Wed, 05 Jun 2013 00:48:12 GMT represent the same time, the first one is GMT (Grenweech) and the second one is the same time in Australia (EST), you just need to configure your time zone properly.

If you want to print the same date in GMT, add this line to your code:

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

Now, if you also want your date to be printed with the time-zone as "GMT" instead of "+0000", follow @HewWolff answer (use zzz instead of ZZZ)

Sign up to request clarification or add additional context in comments.

6 Comments

The question is: does the OP want to output to use the timezone from the original input string, or use a fixed timezone (such as GMT)? I guess setTimeZone is probably part of the solution, but first we need to know the exact problem.
@Mattias Buelens, I intend to output the original input String in a fixed timezone (such as GMT), What Date and Time Pattern should I used here. The expected result should be the same as "Wed, 05 Jun 2013 00:48:12 GMT". Thanks.
@user1304846 use both Hew Wolff answer (to print GMT) and mine (to set the time zone to GMT)
@morgano, thanks for your advice, and I used the code Clocks provided below and replaced 'z' with "zzz' in his code, the result is "Wed, 05 Jun 2013 12:48:12 GMT", which is different to the the input string "Wed, 05 Jun 2013 00:48:12 GMT". Any further suggestion here?
@user1304846 Use HH instead of hh for 24-hours format. Clocks also pointed that out following up on his own answer.
|
1

This solves your problem:

import java.text.*;
import java.util.*;

public class DateIt{
    public static void main(String [] args) throws Exception{
        String dStr= "Wed, 05 Jun 2013 00:48:12 GMT"; 
        SimpleDateFormat ft =  new SimpleDateFormat ("E, dd MMM yyyy HH:mm:ss z"); 
        Date t = ft.parse(dStr); 
        TimeZone gmt = TimeZone.getTimeZone("England/London");
        ft.setTimeZone(gmt);
        System.out.println(t); 
        System.out.println(ft.format(t));
        }
}

3 Comments

thanks, I use your code, but the result is "Wed, 05 Jun 2013 12:48:12 GMT", which is different to the the input string "Wed, 05 Jun 2013 00:48:12 GMT". Any further suggestion here?
Does line 7 have 'HH' instead of 'hh'? 'HH' will return the time in 24 hour notation.
you are right, I was using "hh', after I changed it to "HH", then everything works ok. Many Thanks.
1

It looks like you want zzz rather than ZZZ. That should help you read/write your time zone as a code rather than a number.

Comments

0

Ugh - I've run into this problem before.

The SimpleDateFormat.parse(String) method (I suspect) uses an instance of Calendar. This matters for two reasons:

  1. You don't know which flavor of Calendar you're dealing with (Gregorian? Mayan? Vegan?).
  2. Calendar initializes its fields with default values that you don't necessarily want set. When the SimpleDateFormat calls getTime() on the Calendar instance it created to return a Date, that Date will have default field values that aren't indicated by the string you submitted to begin with.

These default values are having an impact when the ft.format(t) call is made. The only way to solve this problem that I've found is to work with the Calendar class directly (which is entirely non-trivial, BTW.) I'll try to provide a code sample later when I've got more time. In the mean time, look at javafx.util.converter.DateStringConverter.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.