david david - 1 year ago 41
Java Question

Timezone differences with GMT causing hours to change?

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?

Answer Source

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)".

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download