Reputation: 284
I have the following timestamp stored as a String that I would like to parse using SimpleDateFormat
but I'm having some issues with converting:
My TimeStamp which I read in from an xml file: Tue Dec 31 09:29:08 PDT 2013
My Code:
String timeStamp = innerNode.getTextContent(); //innerNode is a Node object that contains my TimeStamp from an xml file.
System.out.println(timeStamp);
SimpleDateFormat dateFormat = new SimpleDateFormat("E MMM dd HH:mm:ss z yyyy");
dateFormat.setTimeZone(TimeZone.getTimeZone("PDT"));
dateFormat.parse(timeStamp);
Date firstParsedDate = dateFormat.parse(timeStamp);
System.out.println(firstParsedDate);
So that works great but my result is strange. Instead of printing Tue Dec 31 09:29:08 PDT 2013
my result is instead: Tue Dec 31 11:29:08 CDT 2013
Any ideas what is wrong? Thanks.
Upvotes: 1
Views: 169
Reputation: 454
A date object in java will use the system time zone. Your system is in CDT timezone and not in PDT. So the date is being printed in CDT.
Upvotes: 1
Reputation: 17309
Date.toString
won't use the time zone you entered for the SimpleDateFormat
that created it. Instead, you should do:
System.out.println(dateFormat.format(firstParsedDate));
This should use the correct time zone that you set on the formatter. toString
just uses the system time zone.
Upvotes: 2