• Post Reply Bookmark Topic Watch Topic
  • New Topic

SimpleDateFormat.parse and SimpleDateFormat.format not producing identical values  RSS feed

 
Kumar Raja
Ranch Hand
Posts: 550
2
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

We are using SimpleDateFormat in JAXB serialization/deserialization process of java.util.Date objects and I'm writing following utility to accomplish that



What I noticed is str1 = "2001-07-04T12:08:56.235-07:00"; and str2 = 2001-07-04T14:08:56.235-05:00

What I need to do to make sure, both dates are same?

Thanks
Kumar
 
Stefan Evans
Bartender
Posts: 1837
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The date is the same, even if the strings are not.
Both of those strings represent the same moment in time - just in different timezones.

The timezone for parsing is specified by the input str1.
The timezone for formatting if not otherwise specified goes with the system default. You can use formatter.setTimeZone to set it.


 
Jelle Klap
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The String you are parsing specifies a -7 hour GMT offset, which is probably Mountain Standard Time (MST). This does not seem to be you default time zone, so you have to set the right TimeZone on the DataFormat prior to formatting the Date:

Remeber that a Date is just a wrapper for a long value that represents the milliseconds since January 1, 1970, 00:00:00 GMT <-- always GMT, it does not account for time zone offsets.
 
Kumar Raja
Ranch Hand
Posts: 550
2
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you all
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!