Java ISODateTimeFormat中的dateTimeNoMillis()格式化程序
我需要验证字符串的格式是否为ISODateTimeFormat.dateTimeNoMillis() 它应该抛出异常,因为日期的格式错误,但事实并非如此。Java ISODateTimeFormat中的dateTimeNoMillis()格式化程序,java,datetime,jodatime,Java,Datetime,Jodatime,我需要验证字符串的格式是否为ISODateTimeFormat.dateTimeNoMillis() 它应该抛出异常,因为日期的格式错误,但事实并非如此。 还有什么我需要补充的吗?没有,你不需要再做什么了 我怀疑它实际上是在抛出示例,但您的消息诊断是不正确的。使用Joda Time 2.1,这对我来说肯定是失败的: import org.joda.time.*; import org.joda.time.format.*; public class Test { public sta
还有什么我需要补充的吗?没有,你不需要再做什么了 我怀疑它实际上是在抛出示例,但您的消息诊断是不正确的。使用Joda Time 2.1,这对我来说肯定是失败的:
import org.joda.time.*;
import org.joda.time.format.*;
public class Test {
public static void main(String[] args) throws Exception {
DateTimeFormatter formatter = ISODateTimeFormat.dateTimeNoMillis();
String s = "2012-W12-12";
try {
DateTime dt = formatter.parseDateTime(s);
System.out.println(dt);
} catch (IllegalArgumentException e) {
System.out.println(e);
}
}
}
输出:
java.lang.IllegalArgumentException: Invalid format: "2012-W12-12" is malformed
at "W12-12"
代码正在“dateHourMinuteSecondFormatter”上应用parseDateTime(),但在“formatter”上没有应用。@user1772643:抱歉-我编辑了代码第一行中的名称以避免水平滚动,后来忘记了应用它。它实际上是在同一个对象上调用它:)请立即尝试代码。
java.lang.IllegalArgumentException: Invalid format: "2012-W12-12" is malformed
at "W12-12"