1、格式化输出
String dateStr = "2023-11-08 11:09:00";
Date date = DateUtil.parse(dateStr);
//format:2023/11/08
String format = DateUtil.format(date, "yyyy/MM/dd");
System.out.println("format:"+ format);
//formatDate:2023-11-08
String formatDate = DateUtil.formatDate(date);
System.out.println("formatDate:" + formatDate);
//formatDateTime:2023-11-08 11:09:00
String formatDateTime = DateUtil.formatDateTime(date);
System.out.println("formatDateTime:"+ formatDateTime);
//formatTime:11:09:00
String formatTime = DateUtil.formatTime(date);
System.out.println("formatTime:"+ formatTime);
2、字符串转日期
DateUtil.parse方法会自动识别一些常用格式,包括:
(1)yyyy-MM-dd HH:mm:ss
(2)yyyy-MM-dd
(3)HH:mm:ss
(4)yyyy-MM-dd HH:mm
(5)yyyy-MM-dd HH:mm:ss.SSS
String dateStr = "2023-11-08";
Date date = DateUtil.parse(dateStr);
System.out.println("date:"+ date);
//也可以使用自定义格式转化
String dateStr = "2023-11-08";
Date date = DateUtil.parse(dateStr, "yyyy-MM-dd");
3、日期时间差
有时候我们需要计算两个日期之间的时间差(相差天数、相差小时数等),
Hutool将此类方法封装为between方法:
Date startDate=DateUtil.parse("2023-11-01 12:25:08");
Date endDate=DateUtil.parse("2023-11-08 18:25:08");
//相差周数
long week=DateUtil.between(startDate,endDate,DateUnit.WEEK);
//相差天数
long day=DateUtil.between(startDate,endDate,DateUnit.DAY);
//相差小时数
long hour=DateUtil.between(startDate,endDate,DateUnit.HOUR);
//相差分钟数
long minute=DateUtil.between(startDate,endDate,DateUnit.MINUTE);
//相差秒数
long second=DateUtil.between(startDate,endDate,DateUnit.SECOND);
//相差毫秒数
long millis=DateUtil.between(startDate,endDate,DateUnit.MS);
4、格式化时间差
使用DateUtil.formatBetween方法:
formatBetween(long betweenMs, Level level) 的第一个参数betweenMs为相差毫秒数
//相差毫秒数
long millis=DateUtil.between(startDate,endDate,DateUnit.MS);
//相差1天(天)
String formatDay = DateUtil.formatBetween(millis, BetweenFormatter.Level.DAY);
//相差1天3小时(小时)
String formatHour = DateUtil.formatBetween(millis, BetweenFormatter.Level.HOUR);
//相差1天3小时10分(分钟)
String formatMinute = DateUtil.formatBetween(millis, BetweenFormatter.Level.MINUTE);
//相差1天3小时10分(秒)
String formatSecond = DateUtil.formatBetween(millis, BetweenFormatter.Level.SECOND);
//相差1天3小时10分(毫秒)
String formatMillis = DateUtil.formatBetween(millis, BetweenFormatter.Level.MILLISECOND);
5、Long和Date互相转化
//Date转Long
Long date = DateUtil.parse("2023-11-08 14:34:33", "yyyy-MM-dd HH:mm:ss").getTime();
//Long转Date
Long longDate=DateUtil.parse("2023-11-08 14:34:33", "yyyy-MM-dd HH:mm:ss").getTime();
Date date = DateUtil.date(longDate);
6、星期几
public static String dayOfWeek(Date date){
int day= DateUtil.dayOfWeek(date);
switch (day){
case 1:return "星期天";
case 2:return "星期一";
case 3:return "星期二";
case 4:return "星期三";
case 5:return "星期四";
case 6:return "星期五";
case 7:return "星期六";
default:return "";
}
}
7、日期时间偏移
日期或时间的偏移指针针对某个日期增加或减少分、小时、天等等,达到日期变更的目的。Hutool也针对其做了大量封装。
String dateStr = "2023-11-08 14:33:23";
Date date = DateUtil.parse(dateStr);
//减3天
DateUtil.offsetDay(date, -3);
DateUtil.offset(date, DateField.DAY_OF_YEAR, -3);
//昨天
DateUtil.yesterday();
//明天
DateUtil.tomorrow();
//上周
DateUtil.lastWeek();
//下周
DateUtil.nextWeek();
//上个月
DateUtil.lastMonth();
//下个月
DateUtil.nextMonth();
8、获取日期时间指定部分
Date date = DateUtil.parse("2023-11-08 14:22:33.100");
//年
int year = DateUtil.year(date);//2023
System.out.println("year:"+ year);
//年和季度
String yearAndQuarter = DateUtil.yearAndQuarter(date);//20234
System.out.println("yearAndQuarter:"+ yearAndQuarter);
//季度
int quarter = DateUtil.quarter(date);//4
System.out.println("quarter:"+quarter);
//月,从0开始
int month = DateUtil.month(date);//10 十一月
System.out.println("month:"+month);
//获得指定日期是所在年份的第几周
int weekOfYear = DateUtil.weekOfYear(date);//46
System.out.println("weekOfYear:"+ weekOfYear);
//一月的第几周,一周的第一天为周一
int weekOfMonth = DateUtil.weekOfMonth(date);//2
System.out.println("weekOfMonth:"+weekOfMonth);
//一周的第几天,从星期天开始,从1开始
int dayOfWeek = DateUtil.dayOfWeek(date);//4 星期三
System.out.println("dayOfWeek:"+dayOfWeek);
//一月的第几天
int dayOfMonth = DateUtil.dayOfMonth(date);//8
System.out.println("dayOfMonth:"+dayOfMonth);
//一年的第几天
int dayOfYear = DateUtil.dayOfYear(date);//312
System.out.println("dayOfYear:"+dayOfYear);
//小时
int hour = DateUtil.hour(date, true);//14
System.out.println("hour:"+hour);
//分钟
int minute = DateUtil.minute(date);//22
System.out.println("minute:"+minute);
//秒
int second = DateUtil.second(date);//33
System.out.println("second:"+second);
//毫秒
int millisecond = DateUtil.millisecond(date);//100
System.out.println("millisecond:"+millisecond);
9、开始时间和结束时间
Date date = DateUtil.parse("2023-11-08 11:22:33.100");
//当前秒的开始时间
Date beginOfSecond = DateUtil.beginOfSecond(date);//2023-11-08 11:22:33
System.out.println("beginOfSecond:"+beginOfSecond);
//当前秒的结束时间
Date endOfSecond = DateUtil.endOfSecond(date);//:2023-11-08 11:22:33
System.out.println("endOfSecond:"+endOfSecond);
//当前分钟的开始时间
Date beginOfMinute = DateUtil.beginOfMinute(date);//2023-11-08 11:22:00
System.out.println("beginOfMinute:"+beginOfMinute);
//当前分钟的结束时间
Date endOfMinute = DateUtil.endOfMinute(date);//2023-11-08 11:22:59
System.out.println("endOfMinute:"+endOfMinute);
//当前小时的开始时间
Date beginOfHour = DateUtil.beginOfHour(date);//2023-11-08 11:00:00
System.out.println("beginOfHour:"+beginOfHour);
//当前小时的结束时间
Date endOfHour = DateUtil.endOfHour(date);//2023-11-08 11:59:59
System.out.println("endOfHour:"+endOfHour);
//当前天的开始时间
Date beginOfDay = DateUtil.beginOfDay(date);//2023-11-08 00:00:00
System.out.println("beginOfDay:"+beginOfDay);
//当前天的结束时间
Date endOfDay = DateUtil.endOfDay(date);//2023-11-08 23:59:59
System.out.println("endOfDay:"+endOfDay);
//当前周的开始时间,
Date beginOfWeek = DateUtil.beginOfWeek(date);//2023-11-06 00:00:00 周一
System.out.println("beginOfWeek:"+beginOfWeek);
//当前周的结束时间
Date endOfWeek = DateUtil.endOfWeek(date);//2023-11-12 23:59:59
System.out.println("endOfWeek:"+endOfWeek);
//当前月的开始时间
Date beginOfMonth = DateUtil.beginOfMonth(date);//2023-11-01 00:00:00
System.out.println("beginOfMonth:"+beginOfMonth);
//当前月的结束时间
Date endOfMonth = DateUtil.endOfMonth(date);//2023-11-30 23:59:59
System.out.println("endOfMonth:"+endOfMonth);
//当前季度的开始时间
Date beginOfQuarter = DateUtil.beginOfQuarter(date);//2023-10-01 00:00:00
System.out.println("beginOfQuarter:"+beginOfQuarter);
//当前季度的结束时间
Date endOfQuarter = DateUtil.endOfQuarter(date);//2023-12-31 23:59:59
System.out.println("endOfQuarter:"+endOfQuarter);
//当前年的开始时间
Date beginOfYear = DateUtil.beginOfYear(date);//2023-01-01 00:00:00
System.out.println("beginOfYear:"+beginOfYear);
//当前年的结束时间
Date endOfYear = DateUtil.endOfYear(date);//2023-12-31 23:59:59
System.out.println("endOfYear:"+endOfYear);