SSM时间格式问题
存入数据库的时间格式是datetime
| time |
|---|
| 2020-01-16 21:32:52 |
然而前端取到的是
1579181572000(一串数字 =_=|| 真的是wtf )
历经十几分钟的百度脑补知识后的我恍然大悟
解决方法如下:
- 先在pom文件中添加jackson依赖和spring依赖(这里我偷懒把spring包一次导入 虽然有些没用的HH)
<!-- spring 3个 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.1.6.RELEASE</version>
</dependency>
<!-- jackson -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.4.1</version>
</dependency>
- 在属性上加入如下两个注解
import org.springframework.format.annotation.DateTimeFormat;
import com.fasterxml.jackson.annotation.JsonFormat;
//接收参数的格式
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
//输出参数的格式
@JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss")
private Date time;
这个博主写的不错https://blog.csdn.net/flymu0808/article/details/50669294
以下这段是摘自他的:
Why do Dates get written as numbers?
Default serializer for java.util.Date (and related, such as java.util.Calendar) serialize them using the most efficient accurate representation, so-called epoch timestamp (number of milliseconds since January 1st, 1970, UTC). Assumption is that if user does not care, we should use efficient and accurate representation to get job bdone
本文详细介绍了在SSM框架中遇到的时间格式问题,当数据库存储的datetime类型在前端显示为毫秒时间戳时的解决方案。通过添加jackson和spring-webmvc依赖,并使用@DateTimeFormat和@JsonFormat注解,可以实现前后端时间格式的一致性。

1979

被折叠的 条评论
为什么被折叠?



