Joda-Time 找不到 class "forPattern()"

Joda-Time cannot find class "forPattern()"

我正在尝试使用 Joda-Time v 2.8.2 格式化日期,我发现所有类似的答案都说使用 forPattern() 方法,但是我使用的版本告诉我没有这样的方法method(),我是不是用错了?还是不推荐使用此方法?如果有,是用什么方法代替的?

相关代码:

static final String DATE_FORMAT = "yyyy-MM-dd HH:mm:ss";


public String timeSince(String dateString) {

    org.joda.time.format.DateTimeFormatter formatter = 
                              new DateTimeFormat.forPattern(DATE_FORMAT);
    Seconds secondsSince = Seconds.secondsBetween(DateTime.parse(dateString, formatter),
                                                      DateTime.now());

    ...
}

cannot find class “forPattern()”是一回事,tells me that there is no such method()是完全不同的东西。

实际发生的是 new Class.Function() 是一个语法错误。

因此,java 感到困惑,它认为您一定是在尝试调用构造函数,所以它告诉您它找不到包含此类构造函数的 class。

解决方案:删除 new.

forPattern 是一个静态方法。删除新关键字

DateTimeFormatter formatter = DateTimeFormat.forPattern(DATE_FORMAT);