Java 如何将 String 对象转换为 Date 对象?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1543481/
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 String object to Date object?
提问by Suresh Chaganti
How can I convert a String object to a Date object?
如何将 String 对象转换为 Date 对象?
I think I need to do something like this:
我想我需要做这样的事情:
Date d=(some conversion ) "String "
Any help would be greatly appreciated.
任何帮助将不胜感激。
采纳答案by KB22
SimpleDateFormat dateFormat = new SimpleDateFormat("dd.MM.yyyy");
Date date = dateFormat.parse("1.1.2001");
For details refer to: SimpleDateFormat documentation
详情参考: SimpleDateFormat 文档
回答by Zappi
you should parse the string with the SimpleDateFormat class
您应该使用 SimpleDateFormat 类解析字符串
回答by user151019
See Sun's Java tutorialand the class SimpleDateFormat
请参阅Sun 的 Java 教程和 SimpleDateFormat 类
回答by jarnbjo
Use a SimpleDateFormat
with a format string, which matches your actual format:
将 aSimpleDateFormat
与格式字符串一起使用,该字符串与您的实际格式相匹配:
SimpleDateFormat sdf =
new SimpleDateFormat("yyyy-MM-dd");
Date d = sdf.parse("2009-10-09");
回答by catwalk
use
用
Date date = DateFormat.getInstance.parse( dateString );
回答by TStamper
java.text.SimpleDateFormatthat extends java.text.DateFormat abstract class.
java.text.SimpleDateFormat扩展 java.text.DateFormat 抽象类。
DateFormat MYDate = new SimpleDateFormat("dd/MM/yyyy");
Date today = MYDate.parse("09/10/2009");
回答by Michael Borgwardt
Date-to-String conversion is a relatively complex parsing operation, not something you can do with a simple cast as you are trying.
日期到字符串的转换是一个相对复杂的解析操作,而不是您在尝试时可以通过简单的强制转换来完成的操作。
You'll have to use a DateFormat. It can be as simple as:
您必须使用DateFormat。它可以很简单:
Date d = DateFormat.getDateInstance().parse("09/10/2009");
But this changes the expected date format depending on the locale settings of the machine it's running on. If you have a specific date format, you can use SimpleDateFormat:
但这会根据运行它的机器的区域设置更改预期的日期格式。如果您有特定的日期格式,则可以使用SimpleDateFormat:
Date d = new SimpleDateFormat("d MMM yyyy HH:mm").parse("4 Jul 2001 12:08");
Note that the parse method will always expect one specific format, and will not try to guess what could be meant if it receives a different format.
请注意, parse 方法将始终期望一种特定的格式,并且不会尝试猜测如果它接收到不同的格式可能意味着什么。
回答by Yasir Shabbir Choudhary
You can convert String object into Date object using this method. and this Java code is tested and running component in my environment.
您可以使用此方法将 String 对象转换为 Date 对象。并且此 Java 代码在我的环境中经过测试并运行组件。
public static Date parseStringAsDate(String dateStr, String format) throws ParseException
{
if(null==dateStr || "".equals(dateStr))
throw new IllegalArgumentException("dateStr must not be null or empty");
DateFormat df = new SimpleDateFormat(format);
return df.parse(dateStr);
}
dateStr = "17/05/2017"
dateStr = "17/05/2017"
format= "dd/MM/yyyy"
格式=“日/月/年”