我正在尝试使用 DateTimeFormat 模式获取当前的 DateTime,但出现异常...
//sets the current date
DateTime currentDate = new DateTime();
DateTimeFormatter dtf = DateTimeFormat.forPattern("dd/MM/YYYY HH:mm").withLocale(locale);
DateTime now = dtf.parseDateTime(currentDate.toString());
我收到此异常,我无法理解谁提供了格式错误的格式
java.lang.IllegalArgumentException: Invalid format: "2017-01-04T14:24:17.674+01:00" is malformed at "17-01-04T14:24:17.674+01:00"
最佳答案
此行 DateTime now = dtf.parseDateTime(currentDate.toString());
不正确,因为您尝试使用默认的 toSring 格式解析日期。您必须解析格式与模式相同的字符串:
DateTime currentDate = new DateTime();
DateTimeFormatter dtf = DateTimeFormat.forPattern("dd/MM/YYYY HH:mm").withLocale(locale);
String formatedDate = dtf.print(currentDate);
System.out.println(formatedDate);
DateTime now = dtf.parseDateTime(formatedDate);
System.out.println(now);
关于java - Joda 日期时间 格式无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41465000/