Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get a Date object from String

DateFormat formatter = new SimpleDateFormat("MM/dd/yyyy' 'HH:mm:ss");
Date d = (Date)formatter.parse(dateTime);
System.out.println("date in controller "+d);

I get the output as

date in controller Mon Dec 31 16:04:57 IST 2012

Please suggest a method to output the date in MM/dd/yyyy HH:mm:ss format. Need the output in date format and not as string so as to store the output in datetime field in mysql db

like image 929
divyanair Avatar asked Oct 06 '22 11:10

divyanair


1 Answers

Need the output in date format and not as string so as to store the output in datetime field in mysql db

After the statement

Date d = (Date)formatter.parse(dateTime);
java.sql.Date sqldate = new java.sql.Date(d.getTime())

you have got a java.util.Date object and you can store it as it is in mysql DB (column type : datetime).

However, when you are printing d, it defaults to the .toString() implementation. I think you expected some output like Date@ but the toString printed in user readable format thats why the confusion.

like image 189
Rahul Avatar answered Oct 08 '22 07:10

Rahul