乔达日期时间格式无效
Joda DateTime Invalid format
我正在尝试使用我的 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);
您使用了错误的格式来解析日期。如果在使用 toString
将日期转换为字符串后打印出您尝试解析的日期,您将得到:
2017-01-04T14:24:17.674+01:00
此日期字符串不符合模式 dd/MM/YYYY HH:mm
。要再次将 currentDate
转换为 DateTime
对象的字符串解析,您必须使用以下模式:
DateTimeFormatter dtf = DateTimeFormat.forPattern("YYYY-MM-dd'T'HH:mm:ss.SSSZ")
.withLocale(locale);
用这个 DateTimeFormatter
解析会得到另一个代表与原始 currentDate
相同时间的实例。
有关 DateTimeFormatter
及其解析选项的更多详细信息,请查看 JavaDoc
我正在尝试使用我的 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);
您使用了错误的格式来解析日期。如果在使用 toString
将日期转换为字符串后打印出您尝试解析的日期,您将得到:
2017-01-04T14:24:17.674+01:00
此日期字符串不符合模式 dd/MM/YYYY HH:mm
。要再次将 currentDate
转换为 DateTime
对象的字符串解析,您必须使用以下模式:
DateTimeFormatter dtf = DateTimeFormat.forPattern("YYYY-MM-dd'T'HH:mm:ss.SSSZ")
.withLocale(locale);
用这个 DateTimeFormatter
解析会得到另一个代表与原始 currentDate
相同时间的实例。
有关 DateTimeFormatter
及其解析选项的更多详细信息,请查看 JavaDoc