Java Joda DateTime格式无效

Java Joda DateTime格式无效,java,datetime,jodatime,Java,Datetime,Jodatime,我试图用我的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.toStr

我试图用我的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())不正确,因为您尝试使用默认的ToString格式解析日期。您必须解析与模式格式相同的字符串:

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);

此行
DateTime now=dtf.parseDateTime(currentDate.toString())不正确,因为您尝试使用默认的ToString格式解析日期。您必须解析与模式格式相同的字符串:

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);

您使用了错误的格式来解析日期。如果使用
toString
将要解析的日期转换为字符串后打印出来,则会得到:

2017-01-04T14:24:17.674+01:00
此日期字符串不符合模式
dd/MM/YYYY HH:MM
。要再次将解析为转换为
currentDate
对象的字符串,必须使用以下模式:

DateTimeFormatter dtf = DateTimeFormat.forPattern("YYYY-MM-dd'T'HH:mm:ss.SSSZ")
                                      .withLocale(locale);
使用此
DateTimeFormatter
进行解析将获得另一个实例,该实例表示与原始
currentDate
相同的时间


有关
DateTimeFormatter
及其解析选项的更多详细信息,请查看您正在使用错误的格式解析日期。如果使用
toString
将要解析的日期转换为字符串后打印出来,则会得到:

2017-01-04T14:24:17.674+01:00
此日期字符串不符合模式
dd/MM/YYYY HH:MM
。要再次将解析为转换为
currentDate
对象的字符串,必须使用以下模式:

DateTimeFormatter dtf = DateTimeFormat.forPattern("YYYY-MM-dd'T'HH:mm:ss.SSSZ")
                                      .withLocale(locale);
使用此
DateTimeFormatter
进行解析将获得另一个实例,该实例表示与原始
currentDate
相同的时间


有关
DateTimeFormatter
及其解析选项的更多详细信息,请查看是否尝试将日期格式化为字符串,还是尝试将字符串解析为datetime对象?它应该是datetime对象,但您是从
datetime
开始的-为什么要将其转换为文本并返回?是否尝试将其转换为文本将日期格式化为字符串,或者您正在尝试将字符串解析为datetime对象?它应该是datetime对象,但您是从一个
datetime
开始的-为什么要将其转换为文本并返回?