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.
4 Answers
you can use this
STR_TO_DATE(string, '%d/%m/%Y')
You can specify the format as per your requirement
1 Comment
PSR
you can use that one before using query
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"));
2 Comments
Michal Borek
You don't need to use external librares in such simple cases.
vikingsteve
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...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.
5 Comments
vikingsteve
Remember that SimpleDateFormat is not thread safe stackoverflow.com/questions/10411944/…
Michal Borek
I don't get the point of your comment. It's not a parallel code.
Michal Borek
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.
vikingsteve
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.
Michal Borek
Dear @vikingsteve, please write to Oracle, because you found serious bug in their OFFICIAL tutorial: java.sun.com/docs/books/tutorial/i18n/format/…