Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I initialize a java.sql.Date variable?

Tags:

java

I'm trying to initialize a variable called startDate using java.sql.Date.

I've tried...

java.sql.date startDate = "02/04/2015"

But it thinks it's a string.

java.sql.date startDate = 02/04/2015

But it thinks it's an int.

java.sql.date startDate = 02-04-2015

But it displays the error "invalid character constant".

How do I properly initialize this variable?

like image 515
UndefinedReference Avatar asked Oct 28 '25 03:10

UndefinedReference


1 Answers

One possible approach is to use a SimpleDateFormat and the java.sql.Date(long) constructor like

DateFormat df = new SimpleDateFormat("MM-dd-yyyy");
java.sql.Date sqlDate = new java.sql.Date(df.parse("02-04-2015").getTime());
like image 95
Elliott Frisch Avatar answered Oct 29 '25 18:10

Elliott Frisch



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!