-1

Below is my input date string format:

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

I have to convert above string date in the format as shown below:

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

And I got below code:

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 yyyy", Locale.US);
sdf.setTimeZone(TimeZone.getTimeZone("GMT-07:00"));

System.out.println(sdf.format(date));

And this is what it prints out on the console. I am not sure why it prints different hour value as compared to what I have above in the desired output. It should print out 15 but it is printing 03.

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

I am not sure what is the reason why hours are getting changed because of timezone difference with GMT?

john
  • 11,311
  • 40
  • 131
  • 251

2 Answers2

2

That is the same time except in the first format you are using "HH" for hour that is "Hour in day (0-23)" and second format uses "hh" that is "Hour in am/pm (1-12)".

tsolakp
  • 5,858
  • 1
  • 22
  • 28
0

As the other Answer correctly states, your formatting pattern used incorrect characters.

Let's look at an alternative modern approach.

ISO 8601

Your input string, once decoded to restore the COLON characters, is in standard ISO 8601 format.

URLDecoder.decode("2025-08-08T15%3A41%3A46", "UTF-8")

2025-08-08T15:41:46

Using java.time

You are using troublesome old date-time classes, now legacy, supplanted by the java.time classes.

The java.time classes use ISO 8601 by default when parsing/generating strings.

Your input string lacks any indication of offset-from-UTC or time zone. So we parse as a LocalDateTime.

LocalDateTime ldt = LocalDateTime.parse( "2025-08-08T15:41:46" ) 

ldt.toString(): 2025-08-08T15:41:46

If you know the intended time zone, assign a ZoneId to produce a ZonedDateTime.

ZonedDateTime zdt = ldt.atZone( ZoneId.of( "America/Montreal" ) ) ;

zdt.toString(): 2025-08-08T15:41:46-04:00[America/Montreal]


About java.time

The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.

Where to obtain the java.time classes?

The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
  • I am still working with java 7 and cannot switch to Java8. With that in mind what is the best approach then and get the same output as I needed? – john May 22 '17 at 22:17
  • @david Much of the java.time functionality is back-ported to Java 6 & Java 7 in the [***ThreeTen-Backport***](http://www.threeten.org/threetenbp/) project. Produced by the same team that built java.time and Joda-Time. Well worth the bother of adding a library to your project. The legacy classes are wretched. – Basil Bourque May 22 '17 at 22:40