eclipse 如何使用java将日期从Datepicker转换为Mysql DATETIME格式?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16851107/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to convert a date from a Datepicker to Mysql DATETIME format using java?
提问by HANU
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
.
我有一个来自 HTML Datepicker 的日期 (05/15/2013)。我想将此值保存在 mySQL 列中,该列是DATETIME
. 格式应该是yyyy-MM-dd
.
回答by PSR
you can use this
你可以用这个
STR_TO_DATE(string, '%d/%m/%Y')
You can specify the format as per your requirement
您可以根据您的要求指定格式
回答by vikingsteve
You could use joda time with date formatters like this:
您可以将 joda 时间与这样的日期格式化程序一起使用:
DateTime dateTime = DateTime.parse("05/15/2013", DateTimeFormat.forPattern("mm/dd/yyyy"));
String result = dateTime.toString(DateTimeFormat.forPattern("yyyy-mm-dd"));
回答by Michal Borek
You can use java.text.SimpleDateFormat
class, e.g.
您可以使用java.text.SimpleDateFormat
类,例如
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.
您可以在官方Oracle 教程页面上找到更多信息。
回答by HANU
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);