0

I'm using:

@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "dd/MM/yyyy")
private LocalDate date;

But its not working...

What I receive in soapUi is:

"date":    {
  "year": 1995,
  "month": "OCTOBER",
  "dayOfMonth": 10,
  "dayOfWeek": "TUESDAY",
  "era": "CE",
  "dayOfYear": 283,
  "leapYear": false,
  "monthValue": 10,
  "chronology":   {
     "id": "ISO",
     "calendarType": "iso8601"
  }

},

I should not need an objectmapper right?

is there a way to just use annotations?

j.doe
  • 662
  • 4
  • 19
Tiago Machado
  • 355
  • 7
  • 24
  • @JsonFormat(shape=JsonFormat.Shape.STRING, pattern="yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", timezone="GMT") – Vishal Patel Jul 01 '19 at 09:31
  • 4
    Obviously Jackson ignores the annotation, and serialize `LocalDate` as a regular class. Did you [check this](https://stackoverflow.com/questions/28802544/java-8-localdate-jackson-format)? – Benoit Jul 01 '19 at 09:35
  • @VishalPatel the result is the same :( – Tiago Machado Jul 01 '19 at 09:37
  • @TiagoMachado check the below answer – Vishal Patel Jul 01 '19 at 09:40
  • Hello. It's unclear if your problem is serialization (you need to format date for response) or deserialization (you need to parse date from JSON request). – Mickael Jul 01 '19 at 09:41
  • @Benoit i checked that but somehow it didnt work, now i tried again and it works like a charm, must have done some mistake there. thanks a lot bro – Tiago Machado Jul 01 '19 at 09:47
  • 3
    Did you look into [FasterXML/jackson-modules-java8](https://github.com/FasterXML/jackson-modules-java8) yet? Not that I got experience with it myself. – Ole V.V. Jul 01 '19 at 09:48

2 Answers2

0

To deserialization the LocalDateTime of Java8

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateDeserializer;

@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "dd/MM/yyyy")
@JsonDeserialize(using = LocalDateDeserializer.class)
private LocalDate dateOfBirth;

While using the following dependencies in your project.

Maven

<dependency>
   <groupId>com.fasterxml.jackson.core</groupId>
   <artifactId>jackson-databind</artifactId>
   <version>2.9.7</version>
</dependency>
<dependency>
   <groupId>com.fasterxml.jackson.datatype</groupId>
   <artifactId>jackson-datatype-jsr310</artifactId>
   <version>2.9.7</version>
</dependency>

Gradle

compile "com.fasterxml.jackson.core:jackson-databind:2.9.7"
compile "com.fasterxml.jackson.datatype:jackson-datatype-jsr310:2.9.7"

No additional implementation of Deserializer is required.

Vishal Patel
  • 554
  • 6
  • 15
-1

Specifically, @JsonFormat allows you to specify how to format Date and Calendar values according to a SimpleDateFormat format. Instead of using LocalDate, use Date then specify locale as this annotation's parameter.

@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "dd/MM/yyyy", locale = "en_GB")
public Date getCurrentDate() {
    return new Date();
}

Guide to @JsonFormat in Jackson

genxpress
  • 25
  • 8
  • 4
    Isn’t that a step backward? `Date` is poorly designed and long outdated (and despite the name doesn’t represent a date). `LocalDate` is the modern class to use for a date. – Ole V.V. Jul 01 '19 at 09:46