如何使用Hutool工具类中DateUtil呢?
下文笔者讲述Hutool工具类的简介说明,如下所示
1、Date、long、Calendar之间的相互转换
//当前时间
Date date = DateUtil.date();
//当前时间
Date date2 = DateUtil.date(Calendar.getInstance());
//当前时间
Date date3 = DateUtil.date(System.currentTimeMillis());
//当前时间字符串,格式:yyyy-MM-dd HH:mm:ss
String now = DateUtil.now();
//当前日期字符串,格式:yyyy-MM-dd
String today= DateUtil.today();
2、字符串转日期
DateUtil.parse方法会自动识别一些常用格式
包括:
yyyy-MM-dd HH:mm:ss
yyyy/MM/dd HH:mm:ss
yyyy.MM.dd HH:mm:ss
yyyy年MM月dd日 HH时mm分ss秒
yyyy-MM-dd
yyyy/MM/dd
yyyy.MM.dd
HH:mm:ss
HH时mm分ss秒
yyyy-MM-dd HH:mm
yyyy-MM-dd HH:mm:ss.SSS
yyyyMMddHHmmss
yyyyMMddHHmmssSSS
yyyyMMdd
EEE, dd MMM yyyy HH:mm:ss z
EEE MMM dd HH:mm:ss zzz yyyy
yyyy-MM-dd'T'HH:mm:ss'Z'
yyyy-MM-dd'T'HH:mm:ss.SSS'Z'
yyyy-MM-dd'T'HH:mm:ssZ
yyyy-MM-dd'T'HH:mm:ss.SSSZ
String dateStr = "2024-03-01";
Date date1 = DateUtil.parse(dateStr);
Date date2 = DateUtil.parse(dateStr, "yyyy-MM-dd");
3、格式化日期输出
String dateStr = "2024-03-01";
Date date = DateUtil.parse(dateStr);
//结果 2024/03/01
String format = DateUtil.format(date, "yyyy/MM/dd");
//常用格式的格式化,结果:2024-03-01
String formatDate = DateUtil.formatDate(date);
//结果:2024-03-01 00:00:00
String formatDateTime = DateUtil.formatDateTime(date);
//结果:00:00:00
String formatTime = DateUtil.formatTime(date);
4、获取Date对象的某个部分
Date date = DateUtil.date();
//获得年的部分
DateUtil.year(date);
//获得月份,从0开始计数,实际需要加1
DateUtil.month(date);
//获得月份枚举
DateUtil.monthEnum(date)
//获取天
DateUtil.dayOfMonth(date)
//获取时
DateUtil.hour(date, true);
//获取分
DateUtil.minute(date);
//获取秒
DateUtil.second(date);
5、开始和结束时间
String dateStr = "2024-03-01 22:43:13";
Date date = DateUtil.parse(dateStr);
//获取日开始和结束
Date beginOfDay = DateUtil.beginOfDay(date);
Date endOfDay = DateUtil.endOfDay(date);
//获取周开始和结束
DateTime beginOfWeek= DateUtil.beginOfWeek(date);
DateTime endOfWeek= DateUtil.endOfWeek(date);
//获取月开始和结束
DateTime beginOfMonth= DateUtil.beginOfMonth(date);
DateTime endOfMonth= DateUtil.endOfMonth(date);
//获取季度开始和结束
DateTime beginOfQuarter= DateUtil.beginOfQuarter(date);
DateTime endOfQuarter= DateUtil.endOfQuarter(date);
//获取年开始和结束
DateTime beginOfYear = DateUtil.beginOfYear(date);
DateTime endOfYear= DateUtil.endOfYear(date);
6、日期时间偏移
String dateStr = "2024-03-01 22:33:23";
Date date = DateUtil.parse(dateStr);
//结果:2024-03-03 22:33:23
Date newDate = DateUtil.offset(date, DateField.DAY_OF_MONTH, 2);
//常用偏移,结果:2024-03-04 22:33:23
DateTime newDate2 = DateUtil.offsetDay(date, 3);
//常用偏移,结果:2024-03-01 19:33:23
DateTime newDate3 = DateUtil.offsetHour(date, -3);
//昨天
DateUtil.yesterday()
//明天
DateUtil.tomorrow()
//上周
DateUtil.lastWeek()
//下周
DateUtil.nextWeek()
//上个月
DateUtil.lastMonth()
//下个月
DateUtil.nextMonth()
7、日期时间差
有时候我们需要计算两个日期之间的时间差(相差天数、相差小时数等等),Hutool将此类方法封装为between方法:
String dateStr1 = "2024-08-01 20:13:43";
Date date1 = DateUtil.parse(dateStr1);
String dateStr2 = "2024-07-01 20:13:23";
Date date2 = DateUtil.parse(dateStr2);
//相差一个月,31天
long betweenDay = DateUtil.between(date1, date2, DateUnit.DAY);
有时候我们希望看到易读的时间差,比如XX天XX小时XX分XX秒,此时使用DateUtil.formatBetween方法:
//Level.SECOND表示精确到秒
DateUtil.formatBetween(dateTime1, dateTime2, BetweenFormatter.Level.SECOND);
不然原始方法计算两个时间差如下:
public static String timeDistance(Date endTime, Date startTime) {
long nd = 1000 * 24 * 60 * 60;
long nh = 1000 * 60 * 60;
long nm = 1000 * 60;
long ns = 1000;
// 获得两个时间的毫秒时间差异
long diff = endTime.getTime() - startTime.getTime();
// 计算差多少天
long day = diff / nd;
// 计算差多少小时
long hour = diff % nd / nh;
// 计算差多少分钟
long min = diff % nd % nh / nm;
// 计算差多少秒//输出结果
long sec = diff % nd % nh % nm / ns;
String str = "";
if (day > 0) {
str += day + "天";
}
if (hour > 0) {
str += hour + "小时";
}
if (min > 0) {
str += min + "分钟";
}
if (sec > 0) {
str += sec + "秒";
}
return str;
}
8、星座和属相
// "摩羯座",注意月份不能直接用int数值
String zodiac = DateUtil.getZodiac(Month.JANUARY.getValue(), 19);
// "狗"
String chineseZodiac = DateUtil.getChineseZodiac(1994);
9、其他
//年龄
DateUtil.ageOfNow("1990-01-30");
//是否闰年
DateUtil.isLeapYear(2024);
Date date = DateUtil.date();
//计算日期是一年当中第几天
DateUtil.dayOfYear(date );
//计算日期是周几,从0开始,实际要加1
DateUtil.weekOfMonth(date);
//计算日期是一年当中的第几周
DateUtil.weekOfYear(date);
//计算日期属于第几季度
DateUtil.quarter(date);
//获取两个日期中间所有日期,包含开始和结束
public static list<String> rangeDay(DateTime start, DateTime end){
List<String> dates = new LinkedList<>();
DateTime currentDate = start;
while (!currentDate.isAfter(end)) {
dates.add(DateUtil.format(currentDate,"yyyyMMdd"));
currentDate = currentDate.offset(DateField.DAY_OF_MONTH,1);
}
return dates;
}
//获取两个日期中间所有月份,包含开始和结束
public static List<String> rangeMonth(DateTime start, DateTime end){
List<String> dates = new LinkedList<>();
DateTime currentDate = start;
while (!currentDate.isAfter(end)) {
dates.add(DateUtil.format(currentDate,"yyyyMM"));
currentDate = currentDate.offset(DateField.MONTH,1);
}
return dates;
}
//获取两个日期中间所有年份,包含开始和结束
public static List<String> rangeMonth(DateTime start, DateTime end){
List<String> dates = new LinkedList<>();
DateTime currentDate = start;
while (!currentDate.isAfter(end)) {
dates.add(DateUtil.format(currentDate,"yyyy"));
currentDate = currentDate.offset(DateField.YEAR,1);
}
return dates;
}
版权声明
本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。


