File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JDBC and the fly likes SQL dates, seconds since epoch and GMT/BST differences Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "SQL dates, seconds since epoch and GMT/BST differences" Watch "SQL dates, seconds since epoch and GMT/BST differences" New topic
Author

SQL dates, seconds since epoch and GMT/BST differences

John McParland
Ranch Hand

Joined: May 11, 2009
Posts: 92

Hi all,

I've got some code which takes a date or a date/time combination in a String, parses it and creates SQL dates and times. However it seems to produce incorrect results based on GMT and BST differences;



Running on my machine (UK timezone, currently in BST), the testGMTDates fails!




I don't understand why this happens. I'm particularly perplexed that the testGMTDates puts the same date as the testGMTTimes in an earlier month! Any pointers?

Many thanks,

John


Passed: SCJP 6 (90%), SCJD 6
Other: Spring training, extensive Swing experience
John McParland
Ranch Hand

Joined: May 11, 2009
Posts: 92

And within seconds of posting, I feel like a numpty as I can now see the issue (I'll say it's because explaining it to someone else helps clarify the issue);

From testGMTDates;



From testGMTTimes;



The month (6th character of the string) is wrong in dateStr / testGMTDates.
John McParland
Ranch Hand

Joined: May 11, 2009
Posts: 92

As a side note: do not use online epoch converters for Java code, they always gave different times for me depending on whether I was running on a UK or US machine due to GMT / BST differences.

Use Calendar#getInstance(), Calendar#set() and Calendar#getTimeInMillis() to get parsing against the real date / time.
Jan Cumps
Bartender

Joined: Dec 20, 2006
Posts: 2477
    
    7

Thank you for sharing, John.


OCUP UML fundamental and ITIL foundation
youtube channel
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: SQL dates, seconds since epoch and GMT/BST differences
 
Similar Threads
converting sqldates?
Reading the Date and time values from excel sheet
Woa! Why does Date/Calendar do this?
Timestamp
10000 milliseconds showing as 01:00:10.000 - why ?