0

I am unable to deserialize Java-8-LocatDate because of following exception

JSON parse error: Cannot deserialize value of type java.time.LocalDate from String "15/09/1978": Failed to deserialize java.time.LocalDate (java.time.format .DateTimeParseException) Text '15/09/1978' could not be parsed at index 0;

JSON request object containing Date is as follow

[ "employeeName" : "ABC XYZ", "birthDate" : "15/09/1978" ]

I also tried to implement date deserialization by referring to URL Deserialize Java 8 LocalDateTime with JacksonMapper However,following line of code

@DateTimeFormat(iso = DateTimeFormatter.ofPattern("dd/mm/yyyy")) gave following compilation error

Type mismatch: cannot convert from DateTimeFormatter to DateTimeFormat.ISO
in the line

private static final long serialVersionUID = 1L;

@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="EMP_ID", updatable = false, nullable = false)
private long empId;

@Column(name="BIRTH_DATE")
//below line gave compilation error ==> Type mismatch: cannot convert from DateTimeFormatter to DateTimeFormat.ISO
@DateTimeFormat(iso = DateTimeFormatter.ofPattern("dd/mm/yyyy"))
@JsonFormat(shape=JsonFormat.Shape.STRING, pattern="dd/mm/yyyy")
private LocalDate birthDate;

Please help me to resolve this issue, Thanks in advance

Anindya Kar
  • 45
  • 1
  • 7
  • Possible duplicate of [Convert LocalDate in DD/MM/YYYY LocalDate](https://stackoverflow.com/questions/54896499/convert-localdate-in-dd-mm-yyyy-localdate). Or maybe just somehow related. – Ole V.V. Jul 15 '19 at 12:57
  • 1
    Did you look into [jackson-modules-java8](https://github.com/FasterXML/jackson-modules-java8) yet? – Ole V.V. Jul 15 '19 at 13:00

1 Answers1

0

what I would suggest is:

sending the date in terms of milliseconds and changing that String in millisecond to date.

public class DateConverter {

    public static Date getDate(String sessionDate) {
        Long sessionOnDate = Long.parseLong(sessionDate);
        Date date = new Date(sessionOnDate);
        return date;
    }

    public static Long getTimeinMilliseconds(String myDate) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        Date date = sdf.parse(myDate);
        long millis = date.getTime();
        return millis;
    }

}

and in Entity you can simply have:

public class SessionDynamic
{
private Date sessionDate;
}

Let me know:)

DevApp
  • 55
  • 1
  • 7
  • 1
    Please don’t teach the young ones to use the long outdated and notoriously troublesome `SimpleDateFormat` class. At least not as the first option. And not without any reservation. Today we have so much better in [`java.time`, the modern Java date and time API,](https://docs.oracle.com/javase/tutorial/datetime/) and its `DateTimeFormatter`. – Ole V.V. Jul 15 '19 at 12:56
  • 1
    Sure:) I will keep this one and also update the answer with the new edits @OleV.V. – DevApp Jul 15 '19 at 12:58
  • I used - @DateTimeFormat(pattern = "DD/MM/YYYY"). I have Very little knowledge on java.time, the modern Java date and time API. I, also want to use @dateTimeFormat for deserialize value `java.time.LocalDate` from String. Please help – Anindya Kar Jul 15 '19 at 19:21