@JsonFormat(pattern = "yyyy-MM-dd") 转换Date是个时间戳
时间: 2024-03-19 10:41:14 浏览: 24
不完全正确。@JsonFormat(pattern = "yyyy-MM-dd") 是用来指定日期类型属性在序列化为JSON字符串时的格式,不会直接影响到Date类型本身。而Date类型的值是以毫秒数的形式存储,从1970年1月1日00:00:00到指定日期的毫秒数,也称为时间戳。因此,在将Date类型转换为其他日期格式时,需要根据时间戳进行转换。
相关问题
用@JsonFormat(pattern = "yyyy-MM-dd") 转换Date返回前端为什么是个时间戳
@JsonFormat(pattern = "yyyy-MM-dd") 只是用来指定日期类型属性在序列化为JSON字符串时的格式,但是前端接收到的仍然是一个时间戳。因此,如果你在前端看到的是一个时间戳,那是因为在传输过程中,Date类型的值被转换成了时间戳。如果你希望在前端看到指定格式的日期字符串,需要在前端将时间戳转换成字符串格式,例如使用JavaScript中的Date对象的toLocaleDateString()等方法进行转换。
入参实体类date属性上加了@JsonFormat(shape = JsonFormat.Shape.STRING, pattern="yyyy-MM-dd HH:mm:ss",timezone = "CTT"),为什么传的json是时间戳也没报错
可能是因为你在实体类的date属性上使用了@JsonFormat注解,并指定了shape为JsonFormat.Shape.STRING,这将会将时间格式化为字符串类型。然后你指定了pattern参数为"yyyy-MM-dd HH:mm:ss",表示日期时间的格式为"年-月-日 时:分:秒"。此外,timezone参数默认为"CTT"(中国标准时间),表示使用中国的时区。
由于你的日期属性上加了@JsonFormat注解,传入的json数据中即使是时间戳格式,也会被自动解析为字符串类型,并按照指定的格式进行格式化。所以即使传入的json数据是时间戳,也不会报错。
总结起来,通过在实体类的属性上使用@JsonFormat注解,你可以灵活地控制日期的序列化和反序列化方式。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pptx](https://img-home.csdnimg.cn/images/20210720083543.png)
![exe](https://img-home.csdnimg.cn/images/20210720083343.png)