日期转化Json异常🕜
JSON parse error: Cannot deserialize value of type `java.util.Date`
from String \"2022-06-08\": expected format \"yyyy-MM-dd HH:mm:ss\";
nested exception is com.fasterxml.jackson.databind.exc.InvalidFormatException: Cannot deserialize value of type
`java.util.Date` from String \"2022-06-08\": expected format \"yyyy-MM-dd HH:mm:ss\"\n
at [Source: (PushbackInputStream);line: 1, column: 73]
(through reference chain: com.spang.partyManage.party.vo.org.OrgAddAccept[\"creationDate\"])
错误原因🐷
不能解析我传递的日期数据:”2022-06-08“,它默认是要这样的"yyyy-MM-dd HH:mm:ss"。
解决办法🐩
直接在数据库对应的接受类对象和实体类对象上加日期转化的注解。
可能由于种种环境原因,不知道哪个一定正确,下面三个注解任选其一。文章来源:https://www.toymoban.com/news/detail-580934.html
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern="yyyy-MM-dd", timezone = "GMT+8")
@JsonFormat(pattern = "yyyy-MM-dd")
@JsonField(format = "yyyy-MM-dd" )
private Date creationDate;
补充👻
我发现我公司是用的DateUtil。文章来源地址https://www.toymoban.com/news/detail-580934.html
public class DateUtil {
public static final String PATTERN_DATETIME = "yyyy-MM-dd HH:mm:ss";
public static final String PATTERN_DATETIME_MINI = "yyyyMMddHHmmss";
public static final String PATTERN_DATE = "yyyy-MM-dd";
public static final String PATTERN_TIME = "HH:mm:ss";
public static final ConcurrentDateFormat DATETIME_FORMAT = ConcurrentDateFormat.of("yyyy-MM-dd HH:mm:ss");
public static final ConcurrentDateFormat DATETIME_MINI_FORMAT = ConcurrentDateFormat.of("yyyyMMddHHmmss");
public static final ConcurrentDateFormat DATE_FORMAT = ConcurrentDateFormat.of("yyyy-MM-dd");
public static final ConcurrentDateFormat TIME_FORMAT = ConcurrentDateFormat.of("HH:mm:ss");
public static final DateTimeFormatter DATETIME_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
public static final DateTimeFormatter DATETIME_MINI_FORMATTER = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");
public static final DateTimeFormatter DATE_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd");
public static final DateTimeFormatter TIME_FORMATTER = DateTimeFormatter.ofPattern("HH:mm:ss");
......
}
使用的时候:@JsonFormat(pattern = DateUtil.PATTERN_DATE)
到了这里,关于JSON parse error: Cannot deserialize value of type `java.util.Date`的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!