Parse JSON String to Date

时间:2018-06-04 17:24:31

标签: json date parsing objectmapper

I want to parse a JSON String to a date but parsing does not work:

com.fasterxml.jackson.databind.exc.InvalidFormatException: Can not deserialize value of type java.util.Date from String "2006-02-20 05:32:40.0": expected format "yyyy-MM-dd'T'HH:mm:ss.SSSZ" at [Source: { "ORD_START_TIME": "2006-02-20 05:32:40.0", "ENDE_TIME": "2006-02-20 06:15:33.0", }; line: 3, column: 32] (through reference chain: ["ORD_START_TIME"])

I use @JsonFormat for the mapping in the entity class:

@JsonFormat(pattern = "yyyy-MM-dd'T'HH:mm:ss.SSSZ")
@JsonProperty("ORD_START_TIME")
private Date ordStartTime;

Any ideas?

I use Java Spring

1 个答案:

答案 0 :(得分:0)

找到解决方案。有必要为Object Mapper设置以下属性:

ObjectMapper mapper = new ObjectMapper();
mapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS"));