我正在尝试使用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);

10-04 11:41