Results 1 to 1 of 1
- 11-08-2011, 12:26 PM #1Member
- Join Date
- Nov 2011
- Rep Power
Java 7 java.util.Date vs java.sql.Date problem
Hello everybody, I'm having serious problem with the java 7 version Date api. When I try to convert the java.sql.Date to java.util.Date the date is always different of two days. For example in database I have date 2011-11-08, I read it into java.sql.Date and when I try to convert it into java.util.Date - the date is 2011-11-06. I tried following:
new java.util.Date(<java.sql.Date variable>.getTime())
SimpleDateFormat sqlFormat = new SimpleDateFormat("yyyy-MM-dd");
and all of this returns the wrong date. The java.util.Date JavaDoc for Java 7 says following: "Although the Date class is intended to reflect coordinated universal time (UTC), it may not do so exactly, depending on the host environment of the Java Virtual Machine. Nearly all modern operating systems assume that 1 day = 24 × 60 × 60 = 86400 seconds in all cases. In UTC, however, about once every year or two there is an extra second, called a "leap second." The leap second is always added as the last second of the day, and always on December 31 or June 30. For example, the last minute of the year 1995 was 61 seconds long, thanks to an added leap second. Most computer clocks are not accurate enough to be able to reflect the leap-second distinction."
So my question is: could the problem be that java.util.Date in java 7 uses in the long representation of date (the number of milliseconds since epoch) these "leap seconds" but the java.sql.Date does not? Is there any way to correct this behavior? Thanks a lot!
EDIT> I have found out, that the java.sql.Date to java.util.Date is correct - the problem is in the database fetch, where I'm getting the wrong java.sql.Date object in resultSet.getDate() method.
Last edited by pavel_skala; 11-08-2011 at 12:51 PM.
- By Rami in forum Advanced JavaReplies: 4Last Post: 07-27-2011, 12:57 PM
- By Jack in forum New To JavaReplies: 5Last Post: 10-28-2010, 03:59 PM
- By dj kourampies in forum New To JavaReplies: 7Last Post: 01-11-2009, 07:58 PM
- By nn12 in forum New To JavaReplies: 2Last Post: 12-08-2008, 04:42 PM
- By nanaji in forum Advanced JavaReplies: 2Last Post: 08-04-2008, 09:08 AM