gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 20:52:17 24 4
gpt4 key购买 nike

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)

最佳答案

您可以使用 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;
}

}

关于javascript - 将javascript日期转换为java sql日期错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43042520/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com