Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

json date format in spring-boot

I am using spring-boot and I have an entity class defined something like this

import org.joda.time.LocalDateTime;
@Entity
public class Project {

    @Type(type = "org.jadira.usertype.dateandtime.joda.PersistentLocalDateTime")
    private LocalDateTime start_date;
...
...
}

When this class is converted to JSON, the field gets converted to the following string representation

{"start_date":[2014,11,15,0,0,0,0],...., ...}

I want to have the json response as yyyy-MM-dd.

I tried the @DateTimeFormat(iso = ISO.DATE) annotation and that did not help either.

Is there an easy way to do this conversion to proper json format ?

like image 265
Parag Sanghavi Avatar asked Dec 17 '14 00:12

Parag Sanghavi


People also ask

How do you specify date format in spring boot?

To take care send date from client as string object, in format yyyy/MM/dd. In Spring Boot application, to add annotation on the date field with the same format.

How do you format a date in JSON?

JSON does not have a built-in type for date/time values. The general consensus is to store the date/time value as a string in ISO 8601 format.

How do I pass a date field in JSON?

You can get the value of the date field as String by calling the getText() method of JsonParser class and then you can simply convert it into a Date object by using the parse() method of SimpleDateFormat, as you normally parse Date in Java.

What is Java Util date format?

Date format conversion yyyy-mm-dd to mm-dd-yyyy.


3 Answers

There are three things that you need to do to format the date as yyyy-MM-dd:

  1. Add a dependency on com.fasterxml.jackson.datatype:jackson-datatype-joda. Judging by the output you're getting at the moment, I think you may already have this dependency.
  2. Configure Jackson not to format dates as timestamps by adding spring.jackson.serialization.write-dates-as-timestamps: false to your application.properties file.
  3. Annotate the LocalDataTime field or getter method with @JsonFormat(pattern="yyyy-MM-dd")

Note: You'll need to use Spring Boot 1.2 for step 2 to work.

like image 58
Andy Wilkinson Avatar answered Oct 27 '22 14:10

Andy Wilkinson


Without additional dependency - the only thing I had to do is:

  1. To take care send date from client as string object, in format yyyy/MM/dd

  2. In Spring Boot application, to add annotation on the date field with the same format


public class Foo
{
     @JsonFormat(pattern = "yyyy/MM/dd")
     private Date dueDate;
}

Using Spring Boot 2.3.5 version


Update

Another option, instead of step 2, to modify application.properties file, add there the format for any Date object:

spring.jackson.date-format=yyyy/MM/dd

like image 22
Adir D Avatar answered Oct 27 '22 13:10

Adir D


You can use @JsonFormat annotation in and the desired pattern like this without using any dependency :

@JsonFormat(pattern="yyyy-MM-dd")
private Date created_At;
like image 1
Arjun Gautam Avatar answered Oct 27 '22 13:10

Arjun Gautam