-1

I have a date in string format like this. It is coming from some other souce in the below format and I cannot change that:

2025-08-08T15%3A41%3A46

I have to convert above string date in this format now:

Fri Aug 08 15:41:46 GMT-07:00 2025

So below is what I have tried:

String decodedDate = URLDecoder.decode("2025-08-08T15%3A41%3A46", "UTF-8");
SimpleDateFormat dateParser = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss", Locale.US);
Date date = dateParser.parse(decodedDate);
System.out.println(date);

And this is what it prints out on the console. It prints out PDT instead of GMT-07:00. How can I get that?

Fri Aug 08 15:41:46 PDT 2025

Also I am working with Java 7 and I can use joda-time library as well. This conversion method can be called by multiple threads.

In the desired output it is printing out GMT-07:00 so how can I get the timezone also in my code?

Update:-

How about doing this way?

 SimpleDateFormat dateParser = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss", Locale.US);
 TimeZone.setDefault(TimeZone.getTimeZone("GMT-07:00"));
 String decodedDate = URLDecoder.decode("2025-08-08T15%3A41%3A46", "UTF-8");
 Date date = dateParser.parse(decodedDate);
 System.out.println(date.toString());
john
  • 11,311
  • 40
  • 131
  • 251
  • I checked that question and I tried that and still I don't see `GMT-07:00` string in the output. – john May 22 '17 at 19:35
  • 1
    so you want someone to just spoon feed you the *exact* solution? is that it? the duplicate(s) only needs about 2 seconds of documentation reading to figure out what you need to modify to make it exactly like you want. –  May 22 '17 at 19:39
  • no I dont want that. I have tried already those suggestions in the link and I am still not getting the output I need so I am not sure whether those answers answer my question. – john May 22 '17 at 19:45
  • You mention JodaTime, and while not having the experience, I am sure a good solution with JodaTime exists. However, the JodaTime guys suggest you migrate to JSR-310, so why not take that step from the start? – Ole V.V. May 22 '17 at 19:50
  • 1
    just set the default TimeZone to "GMT-07:00" like in `TimeZone.setDefault(TimeZone.getTimeZone("GMT-07:00"))` or use the "X" format (and some additional text) in a second SimpleDateFormat to format the date – user85421 May 22 '17 at 19:52
  • JSR-310 solution: `LocalDateTime.parse(decodedDate).atOffset(ZoneOffset.ofHours(-7)).format(DateTimeFormatter.ofPattern("EEE MMM dd HH:mm:ss OOOO uuuu", Locale.US))`. To use this with Java 7, get the [ThreeTen Backport library](http://www.threeten.org/threetenbp/). – Ole V.V. May 22 '17 at 20:00

1 Answers1

1
SimpleDateFormat dateParser = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss", Locale.US);
String decodedDate = URLDecoder.decode("2025-08-08T15%3A41%3A46", "UTF-8");
Date date = dateParser.parse(decodedDate);

//Decode the given date and convert to Date object

SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd hh:mm:ss z-07:00 yyyy", Locale.US);
sdf.setTimeZone(TimeZone.getTimeZone("GMT"));

System.out.println(sdf.format(date)); // set the timezone and print in the desired format

Output:

Fri Aug 08 07:41:46 GMT-07:00 2025

Update: As suggected by KevinO, a better way to do is

SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd hh:mm:ss z yyyy", Locale.US);
sdf.setTimeZone(TimeZone.getTimeZone("GMT-0700"));
Devendra Lattu
  • 2,732
  • 2
  • 18
  • 27
  • Yeah I already tried that already. My output should be like this: `Fri Aug 08 15:41:46 GMT-07:00 2025` and your output is missing few things. – john May 22 '17 at 19:44
  • @david, I have updated the code. Kindly let me know if it helps. – Devendra Lattu May 22 '17 at 19:51
  • Mostly looks good but your hour values got changed somehow from 15 to 07 why? – john May 22 '17 at 19:54
  • The output should be different for you – Devendra Lattu May 22 '17 at 19:58
  • 1
    If you set `.getTimeZone("GMT-07:00")` for the output, then you do not need to hardcode the -07:00 in the date format. A better approach overall would be to append "-0700" to the decodedDate (though I'm suspicious of the exact value with daylight savings, etc.), and then have the parser use "...:mm:ssZ" to obtain the time. – KevinO May 22 '17 at 20:08
  • @DevendraLattu I got this error with your Update section code. `Unparseable date: "2025-08-08T15:41:46"`. Any thoughts why? – john May 22 '17 at 20:17
  • @david, you need to replace the updated lines only at the bottom parser section. The first date parser should be `yyyy-MM-dd'T'HH:mm:ss` only. – Devendra Lattu May 22 '17 at 20:20
  • Yeah that I fixed it. I am still having same problem. On my computer it prints out this output `Fri Aug 08 03:41:46 GMT-07:00 2025` instead of correct output as in my question. I think somehow hours are getting changed. – john May 22 '17 at 20:31
  • So basically when we change to `GMT-0700` we got the different output. – Devendra Lattu May 22 '17 at 20:34
  • @DevendraLattu I updated the question with my solution and I see right output. Do you think that's the right way? – john May 22 '17 at 20:53
  • That sounds good to me. However, we need to look for different scenarios where `sdf.setTimeZone` and `TimeZone.setDefault` should be preferred. – Devendra Lattu May 22 '17 at 20:57
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/144872/discussion-between-david-and-devendra-lattu). – john May 22 '17 at 20:58