1

I have a problem with date format in JSON response generated in REST project (SpringBoot+Hibernate).

When I call function I got JSON like this:

"rezerwacjaDataOd": 1535580000000,
"rezerwacjaDataDo": 1535839200000,
"rezerwacjaGodzOd": "14:00:00",
"rezerwacjaGodzDo": "12:00:00"

my entity:

private Date rezerwacjaDataOd;
private Date rezerwacjaDataDo;
private Time rezerwacjaGodzOd;
private Time rezerwacjaGodzDo;

It's Date from java.sql and Time from java.sql too

My controller:

@RestController
@CrossOrigin
@RequestMapping("api/rezerwacja")
@Api
public class RezerwacjaController {
...
  @GetMapping(value = "/getRezerwacjaById")
  public @ResponseBody
  Rezerwacja getRezerwacjaById(Integer id) {
      return rezDao.findOne(id);
  }
...

Why Time is in "12:00:00" format, but Date in 1535580000000 format? How to make Date to be in "yyyy-MM-dd" format?

2 Answers 2

6

You should do two things

  • add spring.jackson.serialization.write-dates-as-timestamps:false in your application.properties this will disable converting dates to timestamps and instead use a ISO-8601 compliant format

  • You can then customize the format by annotating the getter method of you dateOfBirth property with @JsonFormat(pattern="yyyy-MM-dd")

Sign up to request clarification or add additional context in comments.

Comments

0

The differences in the way hibernate persists the date/time objects in the database have to do with the way these objects are used.

Per the documentation Time is a thin wrapper around Date that allows the underlying JPA provider to save the date object using the convention your noticed.

On the other hand, the Date object you pass in is converted directly to a timestamp and gets saved this way.

In both cases you can retrieve the value in question and serialize over to the desired format (with ISO-8601 being the best).

Another solution, other than the one mentioned above, is to create a custom serializer to do this.

A simple implementation would be:

public class Iso8601Serializer extends StdSerializer<Date> {

    private static final String ISO_8601_FORMAT = "yyyy-MM-dd'T'HH:mm:ss.SSSZ";

    public Iso8601Serializer() {
        this(null);
    }

    public Iso8601Serializer(Class clazz) {
        super(clazz);
    }

    @Override
    public void serialize(Date date, JsonGenerator jsonGenerator, SerializerProvider serializerProvider)
    throws IOException {
        if (date == null) {
            jsonGenerator.writeNull();
        } else {
            jsonGenerator.writeString(DateFormatUtils.format(date, ISO_8601_FORMAT));
        }
    }

}

Also (and this is a personal thing), I would advise in using plain Date objects to store dates and futhermore, have the respective fields annotated as @Temporal.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.