-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.

4 Answers 4

1

you can use this

STR_TO_DATE

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

You can specify the format as per your requirement

Sign up to request clarification or add additional context in comments.

1 Comment

you can use that one before using query
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"));

2 Comments

You don't need to use external librares in such simple cases.
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...
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.

5 Comments

Remember that SimpleDateFormat is not thread safe stackoverflow.com/questions/10411944/…
I don't get the point of your comment. It's not a parallel code.
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.
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.
Dear @vikingsteve, please write to Oracle, because you found serious bug in their OFFICIAL tutorial: java.sun.com/docs/books/tutorial/i18n/format/…
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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.