简体   繁体   中英

SimpleDateFormat parse always returns EDT date

I have an issue here. I have gone through all the related post but wasnt able to get rid of this situation. I am trying to convert a US/Pacific date from string to a date object:

SimpleDateFormat df = new SimpleDateFormat("dd-MM-yy HH:mm:SS a z");
df.setTimeZone(TimeZoneUtil.getTimeZone("US/Pacific"));
String userTime = df.format(date);// User Time - Returns correct US/Pacific time
Date userDate =  df.parse(userTime); // Always returns the date in EDT

I understand that Date does not have its own format but I am completely foxed to see parse method returning the EDT time.

My question is that I want to convert userTime string to Date object in the same format/time zone that I have set to the SimpleDateFormat. I need help guys.. waiting desperately. Thanks in advance

Looking at the documentation :

Parse
[...]The TimeZone value may be overwritten, depending on the given pattern and the time zone value in text. Any TimeZone value that has previously been set by a call to setTimeZone may need to be restored for further operations.

Hope this could help

java.util.Date objects are not IN a time zone. When you call toString, the string built will be in the local time zone of the jvm.

If you are wanting to then build a string in a different time zone, you need to either construct a new SimpleDateFormat, or set the timezone again before formatting.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM