I am trying to convert a String DateTime
value which is present in a flat file as a Date
object after parsing the flat file in my code.
I have written the code to do that but when I format the date its always giving me a date more than 1 day for the specified value, some times it's adding 5:30.
Below is the code for that:
DateFormat f = new SimpleDateFormat("EEE MMM dd HH:mm:ss zz yyyy");
Date date = f.parse("Tue Aug 23 20:00:03 PDT 2011");
System.out.println("---date----" + date);
The output for the above is
---date----Wed Aug 24 08:30:03 IST 2011
Can you please let me know whats the issue here. Is there a problem in the pattern that I am using in the SimplaDateFormat
class or is there a problem with the code.
I have been scratching my head on this for a long time now.
Because you are not formatting a
date
. Look at the examplewill output
Now, you have a bit modified code
which outputs to
seems that
SimpleDateFormat
don't care about timezone even if'z'
pattern is specified. It is setting the timezone when it parses the input. That's how I can describe that a strange behavior. Then use of'z'
pattern seems obsolete and lead to unpredictable results.so setting the
TimeZone
will fix the issueYour system timezone is different. The output is showing IST - or Indian Standard Time, which is an 12.5h difference from PDT. The code is properly parsing the given date which is PDT (UTC -7) and printing out IST (UTC +5h30).
Java stores Dates as UTC dates. So when you parse the PDT date, Java will convert it to UTC and store it internally as a UTC timestamp. When you print, if you do not specify the timezone, it will default to the system timezone, which in your case would appear to be IST.
To specify an exact timezone, specify it in the SimpleDateFormat:
Sure. You're effectively calling
date.toString()
, which doesn't know anything about theSimpleDateFormat
which was used to parse the original text value. ADate
is just an instant in time. It has no notion of a per-instance format. Additionally, it doesn't know about a time zone. You've given a value in PDT, which was then parsed... and when you print it, it's using the system local time zone (IST). That's whatDate.toString
always does.If you want to format a
Date
in a particular way, using a particular format in a particular time zone, callDateFormat.format
.