Reputation: 10068
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?
Upvotes: 75
Views: 316212
Reputation: 81
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());
Upvotes: 4
Reputation: 1246
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());
Upvotes: 8
Reputation: 634
These are all too long.
Just use:
new Date(System.currentTimeMillis())
Upvotes: 58
Reputation: 338896
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.
)
)
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 ) ;
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.
Upvotes: 3
Reputation: 44
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)
Upvotes: 0
Reputation: 38439
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());
Upvotes: 0
Reputation: 1562
Simply in one line:
java.sql.Date date = new java.sql.Date(Calendar.getInstance().getTime().getTime());
Upvotes: 46
Reputation: 178263
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());
Upvotes: 113