0

I am trying to parse dates to LocalDateTime. Input dates -

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

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

But I got this compilation error

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

Imports -

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

import com.fasterxml.jackson.annotation.JsonFormat;

Did I missed something from Example?

Itsik Mauyhas
  • 3,824
  • 14
  • 69
  • 114

1 Answers1

2

The DateTimeFormatter.ofPattern method is part of the java.time API, which is better than jodatime (written by the same person, and afterwards, with the lessons learned by jodatime as guidance). Jodatime is at this point obsoleted by the java.time API.

Do you have a particularly good reason for continuing to use it? If not, you should move to java.time API instead; it's quite similar so it should not involve much work to do so.

In jodatime, the call is DateTimeFormat.forPattern("yyyy-MM-dd HH:mm"). See the javadoc for joda-time.

rzwitserloot
  • 85,357
  • 5
  • 51
  • 72