2

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

Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
  • _I understand that Date does not have its own format_ It also doesn't have its own timezone. The timezone is part of the formatting. – Sotirios Delimanolis Mar 23 '15 at 22:03
  • possible duplicate of [Converting string to date with timezone](http://stackoverflow.com/questions/4203718/converting-string-to-date-with-timezone) – tzima Mar 23 '15 at 22:16
  • Your `SimpleDateFormat` instance is using a pattern with `z`, which is a General TimeZone[1]. What is the string you are attempting to parse? Is it in EDT? https://docs.oracle.com/javase/8/docs/api/java/text/SimpleDateFormat.html#timezone – Brett Okken Mar 23 '15 at 22:25
  • Hi Brett - 23-03-15 21:28:232 PM PDT I want to parse and create date. I just want to create a date object which has this value. Please note that user can select any date with any timezone so its not constatnt. :) – Atul Shirke Mar 24 '15 at 04:28

2 Answers2

0

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

IlGala
  • 3,331
  • 4
  • 35
  • 49
  • llGala - Thanks for replying. I dnt see any code which is overwriting the timezone. The date that I want to parse is like this - 23-03-15 21:28:232 PM PDT. So I just want to create a date from this. Please note that timezone and date are provided by user at runtime. So it cannot be constant. – Atul Shirke Mar 24 '15 at 04:34
0

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.

Brett Okken
  • 6,210
  • 1
  • 19
  • 25
  • Thanks for replying - " 23-03-15 21:28:232 PM PDT" I want to parse and create date. I just want to create a date object which has this value. Please note that user can select any date with any timezone so its not constatnt. :) – – Atul Shirke Mar 24 '15 at 04:35
  • The date format you are using appears to be correct. What value does it provide? – Brett Okken Mar 24 '15 at 11:57