convert string to joda datetime gets stuck

限于喜欢 提交于 2019-12-13 02:25:44

问题


I'm trying to convert this string: 2014-01-01 00:00:00 to Joda DateTime

I have tried the following:

public static DateTime SimpleIso8601StringToDateTime(String date) {
    DateTimeFormatter df = DateTimeFormat.forPattern(CONSTS_APP_GENERAL.SIMPLE_DATE_FORMAT);
    return df.parseDateTime(date);
}

And also the following:

public static DateTime SimpleIso8601StringToDateTime(String date) {
    DateTime dtDate = DateTime.parse(date, DateTimeFormat.forPattern(CONSTS_APP_GENERAL.SIMPLE_DATE_FORMAT));
    return dtDate;
}

Where

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.

I should mention that this is an Android project.

Any ideas what might be the problem?


回答1:


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.

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.



来源:https://stackoverflow.com/questions/22309923/convert-string-to-joda-datetime-gets-stuck

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!