L-R
L-R

Reputation: 418

Spring Json date and time formatting

I'm looking for some help in formatting a date and time stamp in Spring.

My property is annotated like this:

@JsonFormat(pattern = "dd-MM-yy hh:mm")
@Column(name = "start_time")
private Date startTime;

and then passed into my instance in my data loader like

    DateFormat dateFormat = new SimpleDateFormat("dd-MM-yy hh:mm");
    String startTimeSt = "05-11-2018 12:00";
    Date startTime = null;
    try {
        startTime = dateFormat.parse(startTimeSt);
    } catch (ParseException e) {
        e.printStackTrace();
    }
    Booking booking1 = new Booking(customer1, gemma, startTime);
    bookingRepository.save(booking1);

But it is returned in my json file like

"startTime": "2018-11-05T00:00:00.000+0000",

How do I get the time to show?

I have also tried passing spring.gson.date-format=dd-MM-yy hh:mm into my application.properties file.

Thanks

Upvotes: 0

Views: 1662

Answers (1)

Markoorn
Markoorn

Reputation: 2565

Hours in a date format is specified as HH (uppercase) not hh as in your code example - please try using HH in your format and setting the shape property of the @JsonFormat annotation as follows:

@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "dd-MM-yy HH:mm")

See the following tutorial on @JsonFormat:

https://www.baeldung.com/jackson-jsonformat

Upvotes: 1

Related Questions