2017-08-13 455 views
1

我使用Spring Boot,并尝试在MongoDB中保存一些日期。我输入的日期是无法使用Spring Boot将日期时间保存到MongoDB中

"2017-08-14T12:59"

我同时节省得到这个错误:

Failed to read HTTP message: org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Failed to parse Date value '2017-08-14T12:59': Can not parse date "2017-08-14T12:59.000Z": while it seems to fit format 'yyyy-MM-dd'T'HH:mm:ss.SSS'Z'', parsing fails (leniency? null); nested exception is com.fasterxml.jackson.databind.JsonMappingException: Failed to parse Date value '2017-08-14T12:59': Can not parse date "2017-08-14T12:59.000Z": while it seems to fit format 'yyyy-MM-dd'T'HH:mm:ss.SSS'Z'', parsing fails (leniency? null) (through reference chain: 

在我的POJO我想是这样的:

@JsonDeserialize(using= CustomDateDeserialize.class) 
private Date inputDateTime; 

和我实现解串器是这样的:

private SimpleDateFormat dateFormat = new SimpleDateFormat(
      "yyyy-MM-dd HH:mm"); 

    @Override 
    public Date deserialize(JsonParser paramJsonParser, 
      DeserializationContext paramDeserializationContext) 
      throws IOException, JsonProcessingException { 
     String str = paramJsonParser.getText().trim(); 
     try { 
      return dateFormat.parse(str); 
     } catch (ParseException e) { 

     } 
     return paramDeserializationContext.parseDate(str); 
    } 

什么el我想念这里吗?任何帮助赞赏。

回答

0

您需要在您的反序列化器中修改格式。

SimpleDateFormat dateFormat = new SimpleDateFormat( “yyyy-MM-dd'T'HH:mm”);

任何方式simpledatetimeformat不是线程安全的。如果你用java8使用DateTimeFormat。

+0

集提起我想这也是,但我得到JSON解析错误:无法解析日期值“2017-08 -14T12:59':无法解析日期“2017-08-14T12:59.000Z”:虽然它似乎符合格式'yyyy-MM-dd'T'HH:mm:ss.SSS'Z'',解析失败(leniency?null); – Rohitesh

0

你为什么不尝试Instant

@Field("your_db_id_name") 
private Instant inputDateTime; 

public void setInputDateTime(Instant inputDateTime) { 
    this.inputDateTime = inputDateTime; 
} 

public void getInputDateTime() { 
    return inputDateTime; 
} 

您可以通过使用Instant.now()

+0

不错的一个我会试试这个 – Rohitesh

+0

@Rohitesh如果这解决了你的问题upvote和标记为正确的答案。这对其他人会有价值。 –

相关问题