• Post Reply Bookmark Topic Watch Topic
  • New Topic

stupid DateFormat problem  RSS feed

 
Jessica Sant
Sheriff
Posts: 4313
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So, maybe I don't understand the API correctly, or maybe I'm just having a moment. But anyway...

I'm trying to Format a date from a java.sql.Timestamp ( "2004-05-13 12:12:22.123456789" ), to a pretty word thing ( "May 13, 2004 12:12:22 AM GMT" ).

So when I call the BaseFormX.setIncidentTimestamp(Timestamp) it adds 4 hours onto the time for some reason, and its driving me nuts. I'm on EST, so I guess I'm 4 hours off of GMT and so its trying to be helpful -- but I don't want it to!!

These are the results of the two failing tests:

Expected:May 13, 2004 12:12:22 AM GMT
Actual :May 13, 2004 4:12:22 PM GMT

and

Expected:May 13, 2004 4:12:22 PM GMT
Actual :May 13, 2004 8:12:22 PM GMT

And here's the code and failing tests:

BaseFormX.java

BaseFormXTest.java


[ May 20, 2004: Message edited by: Jessica Sant ]
[ May 20, 2004: Message edited by: Jessica Sant ]
 
Stefan Wagner
Ranch Hand
Posts: 1923
Linux Postgres Database Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Far away from a solution, I see in your Test:



which is never used.
Does that mean something?
 
Jessica Sant
Sheriff
Posts: 4313
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yeah -- in an older version of the code I used a timezone offset when I calculated the setIncidentDateTime() --- I guess I forgot to clean up the testing code.

The timezoneOffset doesn't get used.
 
Lu Battist
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The problem is that when you set the timestamp with a string it takes the also sets the timezone as your local timezone. So even though you know its already in GMT format, this method thinks it is not and must convert it to GMT time. Unfortunately, the Timestamp class lacks a handy dandy method setTimeZone() which would have been nice.

Here's some ugly code to use to get around it.

This will give you the expected result and you can use this idea to get a fix working.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!