我试图使用Joda库的DateTimeFormatter从String中解析DateTime对象。出于某种原因,它在到达解析时区时会抛出异常。Joda DateTimeFormatter设置时区时抛出异常:“格式不正确”
这里是我的代码:
protected static final String DEFAULT_TIMEZONE = "America/Denver";
// Parse the date for the note
DateTimeFormatter dtFormatter = DateTimeFormat.forPattern("MM/dd/yyyy hh:mm a ZZZ");
String dateStr = defaultMonth + "/15/" + defaultYear + " 12:00 am " + DateTimeZone.forID(DEFAULT_TIMEZONE);
try {
DateTime noteDate = dtFormatter.parseDateTime(dateStr);
} catch(Exception e) {
e.printStackTrace();
}
堆栈跟踪:
java.lang.IllegalArgumentException异常:无效的格式为: “2014年11月15日上午12:00美国/丹佛” 在畸形“America/Denver” at org.joda.time.format.DateTimeFormatter.parseDateTime(DateTimeFormatter.java:683) at com.happyjacksoftware.emr.actions.AddreportnotesController.create(AddreportnotesController.java:56) at sun.reflect .NativeMethodAccessorImpl.inv oke0(本机方法) 在sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) 在sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) 在java.lang.reflect.Method.invoke(方法的.java:597)
我真的不采取任何时间作为输入,所以我试图在模式字符串"MM/dd/yyyy ZZZ"
但这并没有工作,我想也许DateTime对象所需的时间是实例化。但是增加时间不起作用。
注:我与模式串"MM/dd/yyyy hh:mm a Z"
和字符串11/15/2014 12:00 am -0700
试试这个和它的工作,但得到的数字偏移是很不方便的我,所以我希望有是使用时区ID的解决方案。
'DateTime noteDate = dtFormatter.parseDateTime(“11/15/2014 12:00 am America/Denver”);'对我很好。你可以尝试运行该特定的代码行吗? – assylias
嗯,是啊甚至试图用上面发布的“硬编码”字符串解析仍然会产生相同的异常消息:'java.lang.IllegalArgumentException:格式无效:“11/15/2014 12:00 am America/Denver”格式不正确在“美国/丹佛”。“我打扫了服务器,可能乔达图书馆出了问题,但没有帮助。 – rawkfist0215
@PopoFibo - 是ZZZ的意思是ZoneID –