Get the current date in java.sql.Date format

JavaJdbcjava.util.date

Java Problem Overview


I need to add the current date into a prepared statement of a JDBC call. I need to add the date in a format like yyyy/MM/dd.

I've try with

DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd");
Date date = new Date();
pstm.setDate(6, (java.sql.Date) date);

but I have this error:

threw exception
java.lang.ClassCastException: java.util.Date cannot be cast to java.sql.Date

Is there a way to obtain a java.sql.Date object with the same format?

Java Solutions


Solution 1 - Java

A java.util.Date is not a java.sql.Date. It's the other way around. A java.sql.Date is a java.util.Date.

You'll need to convert it to a java.sql.Date by using the constructor that takes a long that a java.util.Date can supply.

java.sql.Date sqlDate = new java.sql.Date(utilDate.getTime());

Solution 2 - Java

Simply in one line:

java.sql.Date date = new java.sql.Date(Calendar.getInstance().getTime().getTime());

Solution 3 - Java

These are all too long.

Just use:

new Date(System.currentTimeMillis())

Solution 4 - Java

new java.sql.Date(Calendar.getInstance().getTimeInMillis());

Solution 5 - Java

Since the java.sql.Date has a constructor that takes 'long time' and java.util.Date has a method that returns 'long time', I just pass the returned 'long time' to the java.sql.Date to create the date.

java.util.Date date = new java.util.Date();
java.sql.Date sqlDate = new Date(date.getTime());

Solution 6 - Java

tl;dr

myPreparedStatement.setObject(   // Directly exchange java.time objects with database without the troublesome old java.sql.* classes.
    … ,                                   
    LocalDate.parse(             // Parse string as a `LocalDate` date-only value.
        "2018-01-23"             // Input string that complies with standard ISO 8601 formatting.
    ) 
)

java.time

The modern approach uses the java.time classes that supplant the troublesome old legacy classes such as java.util.Date and java.sql.Date.

For a date-only value, use LocalDate. The LocalDate class represents a date-only value without time-of-day and without time zone.

The java.time classes use standard formats when parsing/generating strings. So no need to specify a formatting pattern.

LocalDate ld = LocalDate.parse( input ) ;

You can directly exchange java.time objects with your database using a JDBC driver compliant with JDBC 4.2 or later. You can forget about transforming in and out of java.sql.* classes.

myPreparedStatement.setObject( … , ld ) ;

Retrieval:

LocalDate ld = myResultSet.getObject( … , LocalDate.class ) ;

About java.time

The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.

Where to obtain the java.time classes?

The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.

Solution 7 - Java

Will do: new Date(Instant.now().toEpochMilli())

Solution 8 - Java

In order to get "the current date" (as in today's date), you can use LocalDate.now() and pass that into the java.sql.Date method valueOf(LocalDate).

import java.sql.Date;
...
Date date = Date.valueOf(LocalDate.now());

Solution 9 - Java

You can achieve you goal with below ways :-

long millis=System.currentTimeMillis();  
java.sql.Date date=new java.sql.Date(millis);  

or

// create a java calendar instance
Calendar calendar = Calendar.getInstance();

// get a java date (java.util.Date) from the Calendar instance.
// this java date will represent the current date, or "now".
java.util.Date currentDate = calendar.getTime();

// now, create a java.sql.Date from the java.util.Date
java.sql.Date date = new java.sql.Date(currentDate.getTime());

Solution 10 - Java

all you have to do is this

    Calendar currenttime = Calendar.getInstance();               //creates the Calendar object of the current time
    Date sqldate = new Date((currenttime.getTime()).getTime());  //creates the sql Date of the above created object
    pstm.setDate(6, (java.sql.Date) date);              //assign it to the prepared statement (pstm in this case)



    

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestiongiozhView Question on Stackoverflow
Solution 1 - JavargettmanView Answer on Stackoverflow
Solution 2 - Javakelevra88View Answer on Stackoverflow
Solution 3 - JavaEthan ConnerView Answer on Stackoverflow
Solution 4 - JavaAmalaView Answer on Stackoverflow
Solution 5 - JavaAExplosionView Answer on Stackoverflow
Solution 6 - JavaBasil BourqueView Answer on Stackoverflow
Solution 7 - JavaAndrey LebedenkoView Answer on Stackoverflow
Solution 8 - JavapamcevoyView Answer on Stackoverflow
Solution 9 - JavadugguView Answer on Stackoverflow
Solution 10 - Javamuhammed aslamView Answer on Stackoverflow