格式化输出
String dateStr = "2021-04-21 01:02:03";
Date date = DateUtil.parse(dateStr);
//结果 2021/04/21
String format = DateUtil.format(date, "yyyy/MM/dd");
//常用格式的格式化,结果:2021-04-21
String formatDate = DateUtil.formatDate(date);
//结果:2021-04-01 01:02:03
String formatDateTime = DateUtil.formatDateTime(date);
//结果:01:02:03
String formatTime = DateUtil.formatTime(date);
字符串转日期
String dateStr = "2021-04-21";
Date date = DateUtil.parse(dateStr);
-
DateUtil.parse
方法会自动识别一些常用格式:-
yyyy-MM-dd
-
HH:mm:ss
-
yyyy-MM-dd HH:mm
-
yyyy-MM-dd HH:mm:ss
-
yyyy-MM-dd HH:mm:ss.SSS
-
-
我们也可以使用自定义日期格式转化:
String dateStr = "2021-04-21"; Date date = DateUtil.parse(dateStr, "yyyy-MM-dd");
日期时间差
计算两个日期的时间差(相差天数、相差小时数等),Hutool将此类方法封装为between方法:
Date startDate=DateUtil.parse("2021-04-20 02:00:00");
Date endDate=DateUtil.parse("2021-04-21 05:10:00");
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); //相差毫秒数
中文格式化时间差
有时候我们希望看到易读的时间差,比如XX天XX小时XX分XX秒,此时使用DateUtil.formatBetween
方法:
Date startDate=DateUtil.parse("2021-04-20 02:00:00");
Date endDate=DateUtil.parse("2021-04-21 05:10:00");
//相差毫秒数
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);
Long、Date互转
-
Date转Long
Long date = DateUtil.parse("2021-05-06", "yyyy-MM-dd").getTime();
-
Long转Date
Long longDate=DateUtil.parse("2021-05-06", "yyyy-MM-dd").getTime(); Date date = DateUtil.date(longDate);
获取星期
/**
* 返回星期几
* @param date
* @return
*/
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 "";
}
}
日期时间偏移
日期或时间的偏移指针对某个日期增加或减少分、小时、天等等,达到日期变更的目的:
String dateStr = "2021-05-25 22: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() //下个月
获取日期时间指定部分
Date date = DateUtil.parse("2021-06-30 11:22:33.100");
int year = DateUtil.year(date); //2021
String yearAndQuarter = DateUtil.yearAndQuarter(date); //20212
int quarter = DateUtil.quarter(date); //2
int month = DateUtil.month(date); //月,从0开始,5 六月
int weekOfYear = DateUtil.weekOfYear(date); //日期是所在年份的第几周,头天为周一,27
int weekOfMonth = DateUtil.weekOfMonth(date); //一月的第几周,一周的第一天为周一,5
int dayOfWeek = DateUtil.dayOfWeek(date); //4 星期三 一周的第几天,从星期天开始,从1开始
int dayOfMonth = DateUtil.dayOfMonth(date); //一月的第几天,30
int dayOfYear = DateUtil.dayOfYear(date); //一年的第181天
int hour = DateUtil.hour(date, true); //11小时
int minute = DateUtil.minute(date); //22分钟
int second = DateUtil.second(date); //33秒
int millisecond = DateUtil.millisecond(date); //100毫秒
开始时间和结束时间
Date date = DateUtil.parse("2021-06-30 11:22:33.100");
//当前秒的开始时间
Date beginOfSecond = DateUtil.beginOfSecond(date); //2021-06-30 11:22:33.000
//当前秒的结束时间
Date endOfSecond = DateUtil.endOfSecond(date); //2021-06-30 11:22:33.999
//当前分钟的开始时间
Date beginOfMinute = DateUtil.beginOfMinute(date); //2021-06-30 11:22:00
//当前分钟的结束时间
Date endOfMinute = DateUtil.endOfMinute(date); //2021-06-30 11:22:59
//当前小时的开始时间
Date beginOfHour = DateUtil.beginOfHour(date); //2021-06-30 11:00:00
//当前小时的结束时间
Date endOfHour = DateUtil.endOfHour(date); //2021-06-30 11:59:59
//当前天的开始时间
Date beginOfDay = DateUtil.beginOfDay(date); //2021-06-30 00:00:00
//当前天的结束时间
Date endOfDay = DateUtil.endOfDay(date); //2021-06-30 23:59:59
//当前周的开始时间,
Date beginOfWeek = DateUtil.beginOfWeek(date); //2021-06-28 00:00:00 周一
//当前周的结束时间
Date endOfWeek = DateUtil.endOfWeek(date); //2021-07-04 23:59:59
//当前月的开始时间
Date beginOfMonth = DateUtil.beginOfMonth(date); //2021-06-01 00:00:00
//当前月的结束时间
Date endOfMonth = DateUtil.endOfMonth(date); //2021-06-30 23:59:59
//当前季度的开始时间
Date beginOfQuarter = DateUtil.beginOfQuarter(date);//2021-04-01 00:00:00
//当前季度的结束时间
Date endOfQuarter = DateUtil.endOfQuarter(date); //2021-06-30 23:59:59
//当前年的开始时间
Date beginOfYear = DateUtil.beginOfYear(date); //2021-01-01 00:00:00
//当前年的结束时间
Date endOfYear = DateUtil.endOfYear(date); //2021-12-31 23:59:59
标签:DateUtil,00,06,Hutool,Date,日期,2021,date
From: https://www.cnblogs.com/horseweed/p/16649544.html