java - Joda 日期时间 格式无效

标签 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.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/

相关文章:

datetime - 我如何 ffmpeg 遍历 10,000 多个带有日期时间戳的文件

php - 时间日志监控系统 PHP/MySQL

jquery - WCF 正在更改从 jQuery .ajax 调用返回的 JSON 格式的日期时间

java - 递增 ArrayList 时出现 IndexOutOfBoundsException

java - java语句的执行顺序

java - 为什么我的Java循环矩阵是错误的?

java - 了解 Etc/GMT 时区

java - 在 JFrame 中移动立方体时显示 X Y 坐标

Java 日期验证 joda 时间

java - 从某个日期到当前日期的日期列表