How to convert a date from a Datepicker to Mysql D

2019-09-21 22:16发布

问题:

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.

回答1:

you can use this

STR_TO_DATE

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

You can specify the format as per your requirement



回答2:

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"));


回答3:

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.



回答4:

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);