将javascript日期转换为java sql date错误

时间:2017-03-27 09:22:00

标签: javascript java jackson

Javascript创建日期:

"created_at": new Date().toISOString().slice(0, 19).replace('T', ' ')

在java中我试图将它映射到pojo类中的java.sql.date:

public class Comment extends Model{

    private Date created_at;

    public Date getCreated_at() {
        return created_at;
    }

    public void setCreated_at(Date created_at) {
        this.created_at = created_at;
    }
}

使用preparedStatment映射时出现错误:

PreparedStatement pst = con.prepareStatement(query, Statement.RETURN_GENERATED_KEYS);
pst.setDate(1, newComment.getCreated_at());

错误:

   org.codehaus.jackson.map.JsonMappingException: Can not construct instance of java.sql.Date from String value '2017-03-27 09:28:40': not a valid representation (error: Can not parse date "2017-03-27 09:28:40": not compatible with any of standard forms ("yyyy-MM-dd'T'HH:mm:ss.SSSZ", "yyyy-MM-dd'T'HH:m
m:ss.SSS'Z'", "EEE, dd MMM yyyy HH:mm:ss zzz", "yyyy-MM-dd"))
 at [Source: java.io.StringReader@1ce91e6; line: 1, column: 16] (through reference chain: com.soul.seeker.models.Comment["created_at"])
        at org.codehaus.jackson.map.JsonMappingException.from(JsonMappingException.java:163)
        at org.codehaus.jackson.map.deser.StdDeserializationContext.weirdStringException(StdDeserializationContext.java:243)
        at org.codehaus.jackson.map.deser.std.StdDeserializer._parseDate(StdDeserializer.java:577)
        at org.codehaus.jackson.map.deser.std.StdDeserializer$SqlDateDeserializer.deserialize(StdDeserializer.java:1086)
        at org.codehaus.jackson.map.deser.std.StdDeserializer$SqlDateDeserializer.deserialize(StdDeserializer.java:1077)
        at org.codehaus.jackson.map.deser.SettableBeanProperty.deserialize(SettableBeanProperty.java:299)
        at org.codehaus.jackson.map.deser.SettableBeanProperty$MethodProperty.deserializeAndSet(SettableBeanProperty.java:414)
        at org.codehaus.jackson.map.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:697)
        at org.codehaus.jackson.map.deser.BeanDeserializer.deserialize(BeanDeserializer.java:580)
        at org.codehaus.jackson.map.ObjectMapper._readMapAndClose(ObjectMapper.java:2732)
        at org.codehaus.jackson.map.ObjectMapper.readValue(ObjectMapper.java:1863)
        at com.soul.seeker.serviceImpl.CommentServiceImpl.createComment(CommentServiceImpl.java:39)
        at com.soul.seeker.Application.lambda$main$7(Application.java:80)
        at spark.ResponseTransformerRouteImpl$1.handle(ResponseTransformerRouteImpl.java:47)

2 个答案:

答案 0 :(得分:2)

您可以使用long来接收时间值。 的javascript:

{"created_at": new Date().getTime()}

和 java bean(你可以处理getter方法):

public class Comment extends Model{

private Long created_at;

public Date getCreated_at() {
    return new Date(created_at);
}

public void setCreated_at(Long created_at) {
    this.created_at = created_at;
}

}

答案 1 :(得分:0)

向字段created_at

添加注释
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd hh:mm:ss")

检查你的图书馆版本 例如,我有,它的工作原理:

  

杰克逊 - 注解-2.8.7   jackson-core-2.8.7 jackson-databind-2.1.4