将字符串转换为日期发布(日期解析) [英] Convert String to Date Issue (Date Parsing)
问题描述
我正在尝试将像"05/30/2012" 这样的字符串转换为与"05/30/2012"类似的日期,并且我使用这些代码.
I am trying to convert a String like "05/30/2012" to a Date as same like "05/30/2012" and im using these codes.
DateFormat formatter = new SimpleDateFormat("MM/dd/yyyy");
Date d1=formatter.parse("05/30/2012");
System.out.println(d1);
但是这些代码的编译结果是:
"2012年5月30日星期三EEST 2012"
我需要"2012年5月30日" .感谢您的帮助.
注意:伙计们正在尝试将String转换为Date.Database中有Date列.我不想更改其格式.我只想在数据库中使用05/30/2012但作为Date类而不是String
But these codes compile result is :
"Wed May 30 00:00:00 EEST 2012"
I need "05/30/2012".Thanks for any help.
Note: Guys iam trying to convert String to Date.I have a Date column in Database.I dont want to change it''s format.I just want 05/30/2012 in my database but as a Date class not a String
推荐答案
请参阅以下CP文章:
格式化用于显示的DateTime-格式字符串说明 [ ^ ]
另请参考 DateTime.Parse方法(字符串) [
Please refer following CP Article:
Formatting a DateTime for display - format string description[^]
also refer DateTime.Parse Method (String)[^]
or else
You can parse user input like this:
DateTime enteredDate = DateTime.Parse(enteredString);
如果字符串具有特定格式,则应使用其他方法:
If you have a specific format for the string, you should use the other method:
DateTime loadedDate = DateTime.ParseExact(loadedString, "d", null);
"d"
代表短日期模式(请参阅 MSDN [将字符串转换为日期 [ JavaScript日期格式 [ Javascript将字符串转换为日期 [类似的讨论 [ ^ ]
希望对您有所帮助!
"d"
stands for the short date pattern (see MSDN[^] for more info) and null specifies that the current culture should be used for parsing the string.
For Java:
Please refer following threads:
Convert String to Date[^]
JavaScript Date Format[^]
JavaScript Data Object Examples: Javascript Convert String to Date[^]
Have a look on this Similar discussion[^]
Hope it helps!
尝试这样:
try like this:
DateTime date = DateTime.Now;
String.Format("{0:MM/dd/yyyy}", date);
u也可以尝试:
u can also try :
DateTime.Now.ToString("MM/dd/yyyy");
即在datetime对象中转换所需的字符串,然后使用.ToString("MM/dd/yyyy").
i.e convert required string in datetime object and then use .ToString("MM/dd/yyyy").
这篇关于将字符串转换为日期发布(日期解析)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!