【问题标题】:Joda DateTimeFormatter - The method ofPattern(String) is undefinedJoda DateTimeFormatter - 模式(字符串)的方法未定义
【发布时间】:2020-07-01 17:44:36
【问题描述】:

我正在尝试将日期解析为 LocalDateTime。 输入日期 -

{
    "meetingTitle":"Test",
    "fromTime":"2018-10-30 12:44",
    "toTime":"2018-10-30 12:44"
}

还有 POJO -

private String meetingTitle;
@DateTimeFormat(iso = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm"))
@JsonFormat(pattern = "YYYY-MM-dd HH:mm")
private LocalDateTime fromTime;
@DateTimeFormat(iso = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm"))
@JsonFormat(pattern = "YYYY-MM-dd HH:mm")
private LocalDateTime toTime;

但是我得到了这个编译错误

The method ofPattern(String) is undefined for the type DateTimeFormatter

进口 -

import org.joda.time.LocalDateTime;
import org.joda.time.format.DateTimeFormatter;
import org.springframework.format.annotation.DateTimeFormat;

import com.fasterxml.jackson.annotation.JsonFormat;

我错过了来自Example 的东西吗?

【问题讨论】:

  • 这个例子不是关于 joda 时间,而是关于 java.time 类。所以它使用java.time.format.DateTimeFormatter

标签: java time


【解决方案1】:

DateTimeFormatter.ofPattern方法是java.timeAPI的一部分,比jodatime好(同一个人写的,后来以jodatime的经验教训为指导)。 Jodatime 目前已被 java.time API 淘汰。

您有特别好的理由继续使用它吗?如果没有,您应该改用java.time API;它非常相似,因此不需要太多工作。

在jodatime,电话是DateTimeFormat.forPattern("yyyy-MM-dd HH:mm")。见the javadoc for joda-time

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-09-04
    • 2015-01-29
    • 1970-01-01
    • 2014-06-11
    • 2014-06-01
    • 2013-11-14
    • 1970-01-01
    相关资源
    最近更新 更多