I have a date (05/15/2013) which is from a HTML Datepicker. I want to save this value in a mySQL column, which is the type of DATETIME
. Format should be yyyy-MM-dd
.
Asked
Active
Viewed 1.2k times
4 Answers
1
you can use this
STR_TO_DATE(string, '%d/%m/%Y')
You can specify the format as per your requirement

PSR
- 39,804
- 41
- 111
- 151
-
you can use that one before using query – PSR May 31 '13 at 06:48
0
You could use joda time with date formatters like this:
DateTime dateTime = DateTime.parse("05/15/2013", DateTimeFormat.forPattern("mm/dd/yyyy"));
String result = dateTime.toString(DateTimeFormat.forPattern("yyyy-mm-dd"));

vikingsteve
- 38,481
- 23
- 112
- 156
-
-
Ok, sure, let's use the broken and non-thread-safe `java.util.Date` just so we don't have to add another include to our project... – vikingsteve May 31 '13 at 08:36
0
You can use java.text.SimpleDateFormat
class, e.g.
String pattern = "dd/MM/yyyy";
SimpleDateFormat formatter = new SimpleDateFormat(pattern);
Date today = new Date();
String output = formatter.format(today);
You can find more on official Oracle tutorial page.

Michal Borek
- 4,584
- 2
- 30
- 40
-
Remember that SimpleDateFormat is not thread safe http://stackoverflow.com/questions/10411944/java-text-simpledateformat-not-thread-safe – vikingsteve May 31 '13 at 07:24
-
-
Am I dreaming? This is bunch of method-scope code. If you make it non-thread safe I'll give you Nobel price for creativity. Thanks for "-1", if it's yours please re-learn java. – Michal Borek May 31 '13 at 08:19
-
A DateFormat rarely stays for very long as a method variable, usually ending up as a field and that's where the thread-safety becomes an issue. – vikingsteve May 31 '13 at 08:35
-
Dear @vikingsteve, please write to Oracle, because you found serious bug in their OFFICIAL tutorial: http://java.sun.com/docs/books/tutorial/i18n/format/simpleDateFormat.html – Michal Borek May 31 '13 at 08:37