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