如何获取一个指定日期的一天起始时间呢: hutool
中有自带的方法:
1
| implementation group: 'cn.hutool', name: 'hutool-all', version: '5.7.16'
|
# 获取当天的开始时间
1
| DateUtil.beginOfDay(new Date())
|
# 获取当天的结束时间
1
| DateUtil.endOfDay(new Date())
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date beginOfDay = DateUtil.beginOfDay(new Date()); Calendar c = Calendar.getInstance(); c.setTime(beginOfDay); c.add(Calendar.DAY_OF_MONTH,-1); Date yesterBeginDay = c.getTime(); String a = simpleDateFormat.format(yesterBeginDay); System.out.println(a);
输出结果:2021-12-15 00:00:00
Date endOfDay = DateUtil.endOfDay(new Date()); c.setTime(endOfDay); c.add(Calendar.DAY_OF_MONTH,-1); Date yesterEndDay = c.getTime(); String b = simpleDateFormat.format(yesterEndDay); System.out.println(b);
输出结果:2021-12-15 23:59:59
|