I have a String stating the date as "2012-12-31" how can I convert it into date format using java and store it MySQL database where the type in MySQL is of date type.
Asked
Active
Viewed 1,860 times
-3
-
`2012-12-31` is already date format or what you mean ? – echo_Me Jul 28 '13 at 13:06
-
What are your table formats? What have you tried? – Gordon Linoff Jul 28 '13 at 13:09
-
If you want to convert String to Date object: http://stackoverflow.com/questions/4216745/java-string-to-date-conversion – Prmejc Jul 28 '13 at 13:09
2 Answers
2
You can do as follows
String string = "2012-12-31";
DateFormat formatter = new SimpleDateFormat("yyyy-mm-dd");
java.util.Date date = formatter.parse(string);
java.sql.Date sqlDate = new java.sql.Date(date.getTime()); // convert java.util.date to java.sql.date

ThiepLV
- 1,219
- 3
- 10
- 21
0
Have a look at SimpleDateFormat
String string = "2012-12-31"; //You have like this now
DateFormat formatter = new SimpleDateFormat("yyyy-mm-dd");
Date date = formatter.parse(string);
System.out.println(date);

Vikas V
- 3,176
- 2
- 37
- 60