Results 1 to 1 of 1
  1. #1
    pavel_skala is offline Member
    Join Date
    Nov 2011
    Rep Power

    Default 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())

    <java.sql.Date variable>.toString()

    SimpleDateFormat sqlFormat = new SimpleDateFormat("yyyy-MM-dd");
    sqlFormat.format(<java.sql.Date variable>)

    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.

Similar Threads

  1. Replies: 4
    Last Post: 07-27-2011, 12:57 PM
  2. java.util.Date vs java.sql.Date
    By Jack in forum New To Java
    Replies: 5
    Last Post: 10-28-2010, 03:59 PM
  3. java.util.Date
    By dj kourampies in forum New To Java
    Replies: 7
    Last Post: 01-11-2009, 07:58 PM
  4. Replies: 2
    Last Post: 12-08-2008, 04:42 PM
  5. java new Date() problem
    By nanaji in forum Advanced Java
    Replies: 2
    Last Post: 08-04-2008, 09:08 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts