🙏废话不多说系列,直接开整🙏
一、前端时间格式化
(1)JavaScript 函数
function dateFormat(fmt, date) {
let ret;
const opt = {
"Y+": date.getFullYear().toString(), // 年
"m+": (date.getMonth() + 1).toString(), // 月
"d+": date.getDate().toString(), // 日
"H+": date.getHours().toString(), // 时
"M+": date.getMinutes().toString(), // 分
"S+": date.getSeconds().toString() // 秒
// 有其他格式化字符需求可以继续添加,必须转化成字符串
};
for (let k in opt) {
ret = newRegExp("(" + k + ")").exec(fmt);
if (ret) {
fmt = fmt.replace(ret[1], (ret[1].length == 1) ? (opt[k]) : (opt[k].padStart(ret[1].length, "0")))
};
};
return fmt;
}
(2)方法调用
let date = newDate();
dateFormat("YYYY-mm-dd HH:MM:SS", date); // 2021-8-25 13:51:00
二、SimpleDateFormat 后端格式化
🦅 注意:这里使用 SimpleDateFormat 是 JDK 8 之前的日期格式化处理方案,在 JDK8以及之后我们可以使用 LocaleDate 等类处理。
(1)核心代码
// 定义时间格式化对象和定义格式化样式
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
// 格式化时间对象
String date = dateFormat.format(new Date())
(2)测试示例
@RequestMapping("/list")
public List<UserInfo> getList() {
// 定义时间格式化对象
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
List<UserInfo> list = userMapper.getList();
// 循环执行时间格式化
list.forEach(item -> {
// 使用预留字段 ctime 接收 createtime 格式化的时间(Date->String)
item.setCtime(dateFormat.format(item.getCreatetime()));
item.setUtime(dateFormat.format(item.getUpdatetime()));
});
return list;
}
三、DateTimeFormatter 格式化
🐺 注意:JDK8以及以后我们可以使用 Joda-Time 第三方依赖,或者我们使用 DateTimeFormatter 来格式化。演示示例:(和 SimpleDateFormat 使用类似)
@RequestMapping("/list")
public List<UserInfo> getList() {
// 定义时间格式化对象
DateTimeFormatter dateFormat = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
List<UserInfo> list = userMapper.getList();
// 循环执行时间格式化
list.forEach(item -> {
// 使用预留字段 ctime 接收 createtime 格式化的时间(Date->String)
item.setCtime(dateFormat.format(item.getCreatetime()));
item.setUtime(dateFormat.format(item.getUpdatetime()));
});
return list;
}
提示:
-
LocalDateTime
来接收 MySQL 中的datetime
类型。 -
DateTimeFormatter
和SimpleDateFormat
在使用上的区别是DateTimeFormatter
是用来格式化 JDK 8 提供的时间类型的,如LocalDateTime
,而SimpleDateFormat
是用来格式化Date
类型的
四、全局时间格式化
🦔注意:以上两种后端格式化的实现都有一个致命的缺点,它们在进行时间格式化的时候,都需要对核心业务类做一定的修改,这就相当为了解决一个问题,又引入了一个新的问题,那有没有简单一点、优雅一点的解决方案呢?
(1)配置 application.yml
# 格式化全局时间字段
spring.jackson.date-format=yyyy-MM-dd HH:mm:ss
# 指定时间区域类型(Greenwich Mean Time (GMT) 格林尼治时间,也叫做世界时间。)
spring.jackson.time-zone=GMT+8
附加信息:
import lombok.Data;
import java.util.Date;
@Data
publicclass UserInfo {
privateint id;
private String username;
private Date createtime;
private Date updatetime;
}
(2)测试演示
@RequestMapping("/list")
public List<UserInfo> getList() {
return userMapper.getList();
}
五、部分时间格式化
import com.fasterxml.jackson.annotation.JsonFormat;
import org.springframework.format.annotation.DateTimeFormat;
import lombok.Data;
import java.util.Date;
@Data
publicclass UserInfo {
privateint id;
private String username;
// 对 createtime 字段进行格式化处理
@JsonFormat(pattern = "yyyy-MM-dd hh:mm:ss", timezone = "GMT+8")
// 入参日期格式化处理
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private Date createtime;
private Date updatetime;
}
🙏至此,非常感谢阅读🙏
相关内容