java DateUtils.parseDate 异常
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6794247/
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
DateUtils.parseDate exception
提问by spentak
I can't seem to get this to work, it says Unknown pattern character - "T"
我似乎无法让它工作,它说未知模式字符 - “T”
Unable to parse the date 2011-07-22T12:01:34.9455820
无法解析日期 2011-07-22T12:01:34.9455820
Date theDate = DateUtils.parseDate(notif.dateStr, new String[]{"yyyy-MM-ddTHH:mm:ss.S"});
This won't work either(which is what I was doing first, and in this case it just gave a parse exception):
这也不起作用(这是我首先要做的,在这种情况下它只是给出了一个解析异常):
Date theDate = DateUtils.parseDate(notif.dateStr);
Where is my error?
我的错误在哪里?
回答by CoolBeans
Try yyyy-MM-dd'T'HH:mm:ss.S
as the format pattern. Example:-
尝试yyyy-MM-dd'T'HH:mm:ss.S
作为格式模式。例子:-
public static void main(String[] args) {
String date = "2011-07-22T12:01:34.9455820";
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.S");
try {
Date dt = format.parse(date);
System.out.println(dt.toString()); //prints Fri Jul 22 14:39:09 CDT 2011
} catch (ParseException e) {
e.printStackTrace();
}
}
回答by sottitron
Your error is that you are not providing a pattern that matches the format.
您的错误是您没有提供与格式匹配的模式。
parseDate
public static Date parseDate(String str,
String[] parsePatterns)
throws ParseException
Parses a string representing a date by trying a variety of different parsers.
The parse will try each parse pattern in turn. A parse is only deemed sucessful
if it parses the whole of the input string. If no parse patterns match,
a ParseException is thrown.