How to convert java.util.Date to java.sql.Date?

I am trying to use a java.util.Date as input and then creating a query with it - so I need a java.sql.Date .

I was surprised to find that it couldn't do the conversion implicitly or explicitly - but I don't even know how I would do this, as the Java API is still fairly new to me.


tl;dr

  • Use java.time classes instead of legacy java.util.Date & java.sql.Date with JDBC 4.2 or later.
  • Convert to/from java.time if inter-operating with code not yet updated to java.time.
  • Example query with PreparedStatement .

    myPreparedStatement.setObject( 
        … ,                                         // Specify the ordinal number of which argument in SQL statement.
        myJavaUtilDate.toInstant()                  // Convert from legacy class `java.util.Date` (a moment in UTC) to a modern `java.time.Instant` (a moment in UTC).
            .atZone( ZoneId.of( "Africa/Tunis" ) )  // Adjust from UTC to a particular time zone, to determine a date. Instantiating a `ZonedDateTime`.
            .toLocalDate()                          // Extract a date-only `java.time.LocalDate` object from the date-time `ZonedDateTime` object.
    )
    

    Details

    If you are trying to work with date-only values (no time-of-day, no time zone), use the LocalDate class rather than java.util.Date .

    java.time

    In Java 8 and later, the troublesome old date-time classes bundled with early versions of Java have been supplanted by the new java.time package. See Oracle Tutorial. Much of the functionality has been back-ported to Java 6 & 7 in ThreeTen-Backport and further adapted to Android in ThreeTenABP.

    A SQL data type DATE is meant to be date-only, with no time-of-day and no time zone. Java never had precisely such a class† until java.time.LocalDate in Java 8. Let's create such a value by getting today's date according to a particular time zone (time zone is important in determining a date as a new day dawns earlier in Paris than in Montréal, for example).

    LocalDate todayLocalDate = LocalDate.now( ZoneId.of( "America/Montreal" ) );  // Use proper "continent/region" time zone names; never use 3-4 letter codes like "EST" or "IST".
    

    At this point, we may be done. If your JDBC driver complies with JDBC 4.2 spec, you should be able to pass a LocalDate via setObject on a PreparedStatement to store into a SQL DATE field.

    myPreparedStatement.setObject( 1 , localDate );
    

    Likewise, use ResultSet::getObject to fetch from a SQL DATE column to a Java LocalDate object. Specifying the class in the second argument makes your code type-safe.

    LocalDate localDate = ResultSet.getObject( 1 , LocalDate.class );
    

    In other words, this entire Question is irrelevant under JDBC 4.2 or later.

    If your JDBC driver does not perform in this manner, you need to fall back to converting to the java.sql types.

    Convert to java.sql.Date

    To convert, use new methods added to the old date-time classes. We can call java.sql.Date.valueOf(…) to convert a LocalDate .

    java.sql.Date sqlDate = java.sql.Date.valueOf( todayLocalDate );
    

    And going the other direction.

    LocalDate localDate = sqlDate.toLocalDate();
    

    Converting from java.util.Date

    While you should avoid using the old date-time classes, you may be forced to when working with existing code. If so, you can convert to/from java.time.

    Go through the Instant class, which represents a moment on the timeline in UTC. An Instant is similar in idea to a java.util.Date . But note that Instant has a resolution up to nanoseconds while java.util.Date has only milliseconds resolution.

    To convert, use new methods added to the old classes. For example, java.util.Date.from( Instant ) and java.util.Date::toInstant .

    Instant instant = myUtilDate.toInstant();
    

    To determine a date, we need the context of a time zone. For any given moment, the date varies around the globe by time zone. Apply a ZoneId to get a ZonedDateTime .

    ZoneId zoneId = ZoneId.of ( "America/Montreal" );
    ZonedDateTime zdt = ZonedDateTime.ofInstant ( instant , zoneId );
    LocalDate localDate = zdt.toLocalDate();
    

    † The java.sql.Date class pretends to be date-only without a time-of-day but actually does a time-of-day, adjusted to a midnight time. Confusing? Yes, the old date-time classes are a mess.


    Nevermind....

    public class MainClass {
    
      public static void main(String[] args) {
        java.util.Date utilDate = new java.util.Date();
        java.sql.Date sqlDate = new java.sql.Date(utilDate.getTime());
        System.out.println("utilDate:" + utilDate);
        System.out.println("sqlDate:" + sqlDate);
    
      }
    
    }
    

    explains it. The link is http://www.java2s.com/Tutorial/Java/0040__Data-Type/ConvertfromajavautilDateObjecttoajavasqlDateObject.htm


    With the other answer you may have troubles with the time info (compare the dates with unexpected results!)

    I suggest:

    java.util.Calendar cal = Calendar.getInstance();
    java.util.Date utilDate = new java.util.Date(); // your util date
    cal.setTime(utilDate);
    cal.set(Calendar.HOUR_OF_DAY, 0);
    cal.set(Calendar.MINUTE, 0);
    cal.set(Calendar.SECOND, 0);
    cal.set(Calendar.MILLISECOND, 0);    
    java.sql.Date sqlDate = new java.sql.Date(cal.getTime().getTime()); // your sql date
    System.out.println("utilDate:" + utilDate);
    System.out.println("sqlDate:" + sqlDate);
    
    链接地址: http://www.djcxy.com/p/18516.html

    上一篇: java.util.Date vs java.sql.Date

    下一篇: 如何将java.util.Date转换为java.sql.Date?