java.text.ParseException:无法解析的日期:将 mm/dd/yyyy 字符串转换为日期
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25872702/
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
java.text.ParseException: Unparseable date: convert mm/dd/yyyy string to a date
提问by Subham Tripathi
when i convert my string object in mm/dd/yyyy
format to Date
it gives me
当我将字符串对象的mm/dd/yyyy
格式转换为Date
它给我
java.text.ParseException: Unparseable date: "09/17/2014"
i am trying to do it like this :
我正在尝试这样做:
String date= "09/17/2014";
DateFormat df = new SimpleDateFormat();
Date journeyDate= (java.sql.Date) df.parse(date);
采纳答案by Jon Skeet
There are several potential problems here:
这里有几个潜在的问题:
- You're not specifying a format
- You're not specifying a locale
- You're not specifying a time zone
- You're trying to cast the return value (which will be a
java.util.Date
reference) to ajava.sql.Date
- that would fail
- 您没有指定格式
- 您没有指定语言环境
- 您没有指定时区
- 您正在尝试将返回值(这将是一个
java.util.Date
引用)java.sql.Date
强制转换为- 这将失败
You want something like:
你想要这样的东西:
DateFormat df = new SimpleDateFormat("MM/dd/yyyy", Locale.US);
df.setTimeZone(...); // Whatever time zone you want to use
Date journeyDate = new java.sql.Date(df.parse(text).getTime());
回答by SparkOn
DateFormat df = new SimpleDateFormat("MM/dd/yyyy",Locale.ENGLISH);
Date journeyDate = df.parse(date); // gives you java.util.Date
If you want java.sql.Date then
如果你想要 java.sql.Date 那么
java.sql.Date sqlDate = new java.sql.Date(journeyDate.getTime());
回答by syed
I have the following exception:
我有以下例外:
java.text.ParseException: Unparseable date
java.text.ParseException:无法解析的日期
System.out.println("inside the XMLGregorianCalendar");
sb = (String) map.get(fieldname);
System.out.println("THis is XMLGReogoriaaaaan calendar"+ sb);
DateFormat df = new SimpleDateFormat("yyyy-MM-dd", Locale.US);
Date journeyDate = new java.sql.Date(df.parse(sb).getTime());
System.out.println("this"+journeyDate);
回答by Hrushi
You have mixed m
and M
.
你混合了m
和M
。
m
stands for minute and M
for month.
m
代表分钟和M
月份。
Below is an example of a working format.
下面是一个工作格式的例子。
SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yyyy");
String dateInString = "07/06/2013";
Date date = formatter.parse(dateInString);
System.out.println(formatter.format(date));