将字符串转换为joda datetime卡住 [英] convert string to joda datetime gets stuck
问题描述
我正尝试将以下字符串转换为: 2014-01-01 00:00:00
到Joda DateTime
I'm trying to convert this string: 2014-01-01 00:00:00
to Joda DateTime
我尝试了以下操作:
public static DateTime SimpleIso8601StringToDateTime(String date) {
DateTimeFormatter df = DateTimeFormat.forPattern(CONSTS_APP_GENERAL.SIMPLE_DATE_FORMAT);
return df.parseDateTime(date);
}
还有以下内容:
public static DateTime SimpleIso8601StringToDateTime(String date) {
DateTime dtDate = DateTime.parse(date, DateTimeFormat.forPattern(CONSTS_APP_GENERAL.SIMPLE_DATE_FORMAT));
return dtDate;
}
其中
public static final String SIMPLE_DATE_FORMAT = "yyyy-MM-dd HH:mm:ss.SSS";
但是,使用调试器到达格式化行,然后在尝试处理程序游标时
However, using the debugger I get to the formatting line and then while trying to process it the program cursor never comes back.
我应该提到这是一个Android项目。
I should mention that this is an Android project.
任何想法都可能是
推荐答案
这是因为 2014-01-01 00:00:00
与模式 yyyy-MM-dd HH:mm:ss.SSS
不匹配-输入的秒数没有小数部分。
It’s because 2014-01-01 00:00:00
doesn’t match the pattern yyyy-MM-dd HH:mm:ss.SSS
—there’s no fractional part on the seconds in your input.
结果是引发了未处理的异常-我不熟悉Android如何处理这些异常,除非您设置处理程序。但是将 parse()
调用放在 try
块中应该可以让您恢复。
The result is that an unhandled exception gets raised—I’m not familiar with how Android handles those, the thread probably just dies unless you set a handler. But putting the parse()
call inside a try
block should let you recover.
这篇关于将字符串转换为joda datetime卡住的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!