4

I have a value coming in from JSON payload as:

"callStartTime" : "2019-03-27 13:00:00"

Entity.java

@JsonProperty("callStartTime")
@Column(name = "call_start_dt", nullable = false)
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", lenient = OptBoolean.FALSE)
private Date callStartTime;

When I printed it on console, it says:

Wed Mar 27 08:00:00 CDT 2019

I wanted to be the same as it was in json payload. How I can fix it?

I am just taking date from json and writing it to mysql db in a datetime column.

Utkarsh Gandhi
  • 617
  • 2
  • 6
  • 13
  • Take a look on this question: [Spring Boot Jackson date and timestamp Format](https://stackoverflow.com/questions/55256567/spring-boot-jackson-date-and-timestamp-format) – Michał Ziober Mar 27 '19 at 21:43

4 Answers4

3

Simple solution: I solved it by changing the data type to String which completes my aim to capture the value as it is coming from JSON payload. Using Date and other data types were converting the value to some different timezone.

@JsonProperty("callStartTime")
@Column(name = "call_start_dt", nullable = false)
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", lenient = OptBoolean.FALSE)
private **String** callStartTime;
Utkarsh Gandhi
  • 617
  • 2
  • 6
  • 13
0

Try this

@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd'T'HH:mm:ss.SSSXXX")
private Date callStartTime;
codiallo
  • 173
  • 4
  • It's throwing exception. `exception is com.fasterxml.jackson.databind.exc.InvalidFormatException: Cannot deserialize value of type `java.util.Date` from String "2019-03-27T13:00:00Z": expected format "yyyy-MM-dd'T'HH:mm:ss.SSSXXX"` – Utkarsh Gandhi Mar 27 '19 at 21:42
  • Ah You don't have milliseconds in your date use yyyy-MM-dd'T'HH:mm:ss.XXX – codiallo Mar 27 '19 at 21:44
  • 1
    If you use java version >= 1.8 use ZonedDateTime instead of Date @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd'T'HH:mm:ss.XXX") private ZonedDateTime callStartTime; – codiallo Mar 27 '19 at 21:48
  • Still it throws an exception. same as above mentioned – Utkarsh Gandhi Mar 27 '19 at 23:03
0

java.util.Date does not capture time zone data. It only knows about number of millis since the epoch.

You could try using one of the modules in jackson-modules-java8 and deserialize into an instance of ZonedDateTime instead, which is time-zone aware.

EDIT: try this as a basis for getting it to work:

public class SoTest {
    public static void main(String[] args) throws Exception {
        ObjectMapper om = new ObjectMapper().registerModule(new ParameterNamesModule())
                                            .registerModule(new JavaTimeModule());

        String s = "{\"callStartTime\" : \"2019-03-27T13:00:00Z\" }";

        MyType mt = om.readValue(s, MyType.class);

        System.out.println(mt.getCallStartTime());
    }
}

class MyType {
    @JsonFormat(pattern = "yyyy-MM-dd'T'HH:mm:ssX", lenient = OptBoolean.FALSE)
    private ZonedDateTime callStartTime;

    public ZonedDateTime getCallStartTime() {
        return callStartTime;
    }

    public void setCallStartTime(ZonedDateTime date) {
        this.callStartTime = date;
    }

}
Not a JD
  • 1,864
  • 6
  • 14
  • Got an exception. `Cannot deserialize value of type java.time.ZonedDateTime from String "2019-03-27T13:00:00Z": Text '2019-03-27T13:00:00Z' could not be parsed, unparsed text found at index 19; nested exception is com.fasterxml.jackson.databind.exc.InvalidFormatException: Cannot deserialize value of type java.time.ZonedDateTime from String "2019-03-27T13:00:00Z": Text '2019-03-27T13:00:00Z' could not be parsed, unparsed text found at index 19` – Utkarsh Gandhi Mar 27 '19 at 21:54
  • Also added the all 3 dependencies mentioned in [jackson-modules-8](https://github.com/FasterXML/jackson-modules-java8) – Utkarsh Gandhi Mar 27 '19 at 21:55
  • I am not using ObjectMapper. How do it register it? I am just taking date from json and writing it to mysql db in a datetime column. – Utkarsh Gandhi Mar 27 '19 at 21:56
  • Your use of @JsonProperty (https://fasterxml.github.io/jackson-annotations/javadoc/2.8/com/fasterxml/jackson/annotation/JsonProperty.html) suggests that something somewhere is using Jackson to do JSON parsing which in turn means ObjectMapper is being used. – Not a JD Mar 27 '19 at 21:57
  • okay got it. But still getting this exception. How do I resolve it? – Utkarsh Gandhi Mar 27 '19 at 21:59
  • Try using "@JsonFormat(pattern = "yyyy-MM-dd'T'HH:mm:ssZ", lenient = OptBoolean.FALSE)" - note the Z, to capture timezone (Zulu in the example case you gave above) – Not a JD Mar 27 '19 at 22:00
  • OK - added a small working test. Looks like X was needed, not Z, for your format per https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html – Not a JD Mar 27 '19 at 23:18
-1

There are two possible solutions for this :

1. Define ObjectMapper bean and set date format.

@Bean
public ObjectMapper objectMapper()
{
   ObjectMapper objectMapper = new ObjectMapper();

   DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
   objectMapper.setDateFormat(df);
       return objectMapper;
}

2. Set date format for the particular field using @JsonFormat

@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", lenient = OptBoolean.FALSE)
private Date createTime;
Pramod H G
  • 1,513
  • 14
  • 17