How to convert a date from a Datepicker to Mysql D

2019-09-21 22:17发布

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条回答
混吃等死
2楼-- · 2019-09-21 22:23

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.

查看更多
Deceive 欺骗
3楼-- · 2019-09-21 22:26

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"));
查看更多
甜甜的少女心
4楼-- · 2019-09-21 22:33

you can use this

STR_TO_DATE

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

You can specify the format as per your requirement

查看更多
Rolldiameter
5楼-- · 2019-09-21 22:44

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);
查看更多
登录 后发表回答