-3

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.

HANU
  • 535
  • 1
  • 6
  • 14

4 Answers4

1

you can use this

STR_TO_DATE

STR_TO_DATE(string, '%d/%m/%Y')

You can specify the format as per your requirement

PSR
  • 39,804
  • 41
  • 111
  • 151
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
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
  • I don't get the point of your comment. It's not a parallel code. – Michal Borek May 31 '13 at 08:12
  • 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
0

You can try this

String new_date_string = your_date_string.replace("/", "-");
System.out.println(str);

or you can use regex

SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
String formattedDate = formatter.format(new_date_string);
RAS
  • 8,100
  • 16
  • 64
  • 86
HANU
  • 535
  • 1
  • 6
  • 14