• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

SimpleDateFormat.parse and SimpleDateFormat.format not producing identical values

 
Ranch Hand
Posts: 558
2
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 1845
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.


 
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 558
2
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you all
 
reply
    Bookmark Topic Watch Topic
  • New Topic