Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/323.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 无法使用Spring引导将datetime保存到MongoDB_Java_Mongodb_Spring Boot - Fatal编程技术网

Java 无法使用Spring引导将datetime保存到MongoDB

Java 无法使用Spring引导将datetime保存到MongoDB,java,mongodb,spring-boot,Java,Mongodb,Spring Boot,我使用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

我使用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);
    }

我还错过了什么?非常感谢您的帮助。

您需要修改反序列化程序中的格式

SimpleDataFormat dateFormat=新SimpleDataFormat( “yyyy-MM-dd'HH:MM”)


simpledatetimeformat无论如何都不是线程安全的。如果您使用java8的DateTimeFormat。

您需要修改反序列化程序中的格式

SimpleDataFormat dateFormat=新SimpleDataFormat( “yyyy-MM-dd'HH:MM”)


simpledatetimeformat无论如何都不是线程安全的。如果您使用java8的DateTimeFormat。

为什么不试试
即时
库呢

@Field("your_db_id_name")
private Instant inputDateTime;

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

public void getInputDateTime() {
     return inputDateTime;
}

您可以使用
Instant.now()

设置归档,为什么不试试
Instant
库呢

@Field("your_db_id_name")
private Instant inputDateTime;

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

public void getInputDateTime() {
     return inputDateTime;
}

您可以使用
Instant.now()

设置归档,我也尝试过,但是我得到了JSON解析错误:解析日期值'2017-08-14T12:59'失败:无法解析日期“2017-08-14T12:59.000Z”:虽然它似乎适合格式'yyyy-MM-dd'T'HH:MM:ss.SSS'Z',解析失败(Lenence?null);我也尝试了这一点,但我得到了JSON解析错误:未能解析日期值“2017-08-14T12:59”:无法解析日期“2017-08-14T12:59.000Z”:虽然它似乎适合格式“yyy-MM-dd'T'HH:MM:ss.SSS'Z”,但解析失败(Lenence?null);好的,我试试this@Rohitesh如果这解决了您的问题,请投票并标记为正确答案。这对其他人来说很有价值。好的,我会试试this@Rohitesh如果这解决了您的问题,请投票并标记为正确答案。这对其他人来说是有价值的。