hutool常用日期功能
# (零)hutool maven安装
hutool maven安装
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.8.16</version>
</dependency>
1
2
3
4
5
6
2
3
4
5
6
# (一) 时间格式化
DateUtil.format(new Date(), DatePattern.NORM_DATETIME_PATTERN);
DateUtil.format(new Date(), "yyyy-MM-dd");
1
2
2
# (二) 字符串转时间
DateUtil.parse("2022-12-12 12:12:12");
1
# (三) 时间比较
// 比较结果,如果date1 < date2,返回数小于0,date1==date2返回0,date1 > date2 大于0
DateUtil.compare(DateUtil.parse("2022-12-12 12:12:12"),DateUtil.parse("2022-12-13 12:12:12"))
1
2
2
# (四) 日期偏移
// 向后偏移一天
DateUtil.offsetDay(new Date(), 1);
// 向前偏移一天
DateUtil.offsetDay(new Date(), -1);
1
2
3
4
2
3
4
# (五) 常用
// 今天
DateUtil.now()
// 昨天
DateUtil.yesterday()
// 明天
DateUtil.tomorrow()
// 获取某月的开始时间
DateUtil.beginOfMonth(new Date())
DateUtil.beginOfMonth(DateUtil.parse("2022-12-13 12:12:12"))
// 获取某月的结束时间
DateUtil.endOfMonth(new Date())
DateUtil.beginOfMonth(DateUtil.parse("2022-12-13 12:12:12"))
// 获取年
DateUtil.endOfMonth(new Date())
DateUtil.beginOfMonth(DateUtil.parse("2022-12-13 12:12:12"))
// 获取月份
DateUtil.month(new Date());
DateUtil.beginOfMonth(DateUtil.parse("2022-12-13 12:12:12"))
// 两个日期相差几天
DateUtil.between(new Date(),new Date(), DateUnit.DAY);
DateUtil.between(DateUtil.parse("2021-12-13 12:12:12"),DateUtil.parse("2022-12-13 12:12:12"), DateUnit.DAY);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
上次更新: 2024/01/07, 07:44:52