<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.8.9</version>
</dependency>
class 各种格式的时间相互转换{
public static void main(String[] args) throws ParseException {
String dateStr = "2017-03-01T11:12:12";
/**
* DateUtil.parse进行转换时最好带上原来的格式,否则遇到中间带T格式的时间时,转换后的时间会自动加上8小时
* 比如2017-03-01T11:12:12转换后的时间就是2017-03-01 19:12:12
*/
Date date = DateUtil.parse(dateStr,"yyyy-MM-dd'T'HH:mm:ss");
String format = DateUtil.format(date, "yyyy/MM/dd/HH/mm/ss");
String formatDate = DateUtil.formatDate(date);
String formatDateTime = DateUtil.formatDateTime(date);
String formatTime = DateUtil.formatTime(date);
System.out.println("date:"+date);
System.out.println("format:"+format);
System.out.println("formatDate:"+formatDate);
System.out.println("formatDateTime:"+formatDateTime);
System.out.println("formatTime:"+formatTime);
System.out.println("时区转换:"+DateUtil.convertTimeZone(date, TimeZone.getTimeZone("UTC")));
}
}
class 获取任意时间{
public static void main(String[] args) {
Date date = DateUtil.date();//当前时间,格式:yyyy-MM-dd HH:mm:ss
Date date2 = DateUtil.date(Calendar.getInstance());//当前时间,格式:yyyy-MM-dd HH:mm:ss
Date date3 = DateUtil.date(System.currentTimeMillis());//当前时间,格式:yyyy-MM-dd HH:mm:ss
String now = DateUtil.now();//当前时间字符串,格式:yyyy-MM-dd HH:mm:ss
String today= DateUtil.today();//当前日期字符串,格式:yyyy-MM-dd
System.out.println("获取当前时间date:"+date);
System.out.println("获取当前时间date2:"+date2);
System.out.println("获取当前时间date3:"+date3);
System.out.println("获取当前时间now:"+now);
System.out.println("获取今天日期today:"+today);
System.out.println("获取昨天日期yesterday:"+DateUtil.yesterday());
System.out.println("获取明天日期yesterday:"+DateUtil.tomorrow());
System.out.println("获取上周日期yesterday:"+DateUtil.lastWeek());
System.out.println("获取下周日期yesterday:"+DateUtil.nextWeek());
System.out.println("获取上个月日期yesterday:"+DateUtil.lastMonth());
System.out.println("获取下个月日期yesterday:"+DateUtil.nextMonth());
System.out.println("月份的开始:"+DateUtil.beginOfMonth(DateUtil.yesterday()));
System.out.println("月份的结束:"+DateUtil.endOfMonth(DateUtil.yesterday()));
System.out.println("一天的开始:"+DateUtil.beginOfDay(date));
System.out.println("一天的结束:"+DateUtil.endOfDay(date));
System.out.println("小时的开始:"+DateUtil.beginOfHour(date));
System.out.println("小时的结束:"+DateUtil.endOfHour(date));
System.out.println("季度的开始:"+DateUtil.beginOfQuarter(date));
System.out.println("季度的结束:"+DateUtil.endOfQuarter(date));
System.out.println("一周的开始(从周一开始):"+DateUtil.beginOfWeek(date,true));
System.out.println("一周的开始结束(周日为结束):"+DateUtil.endOfWeek(date,true));
System.out.println("一年的开始:"+DateUtil.beginOfYear(date));
System.out.println("一年的结束:"+DateUtil.endOfYear(date));
}
}
class 获取Date对象的某个部分{
public static void main(String[] args) {
String dateStr = "2022-01-08 18:12:12";
Date date = DateUtil.parse(dateStr,"yyyy-MM-dd HH:mm:ss");
System.out.println("年year:"+DateUtil.year(date));
System.out.println("月month(从0开始计数):"+DateUtil.month(date));//2
System.out.println("获得指定日期是这个日期所在月份的第几天:"+DateUtil.dayOfMonth(date));
System.out.println("24小时hour:"+DateUtil.hour(date, true));
System.out.println("小时(最大为12)hour1:"+DateUtil.hour(date, false));
System.out.println("分minute:"+DateUtil.minute(date));
System.out.println("秒second:"+DateUtil.second(date));
System.out.println("获得指定日期是星期几,1表示周日,2表示周一:"+DateUtil.dayOfWeek(date));
System.out.println("获得指定日期是星期几:"+DateUtil.dayOfWeekEnum(date));
System.out.println("获得指定日期是这个日期所在年的第几天:"+DateUtil.dayOfYear(date));
System.out.println("获得本月的最后一天:"+DateUtil.getLastDayOfMonth(date));
System.out.println("获得指定月份的总天数:"+DateUtil.lengthOfMonth(2,true));
System.out.println("获得指定年份的总天数:"+DateUtil.lengthOfYear(2023));
System.out.println("获得指定日期的毫秒数部分:"+DateUtil.millisecond(date));
System.out.println("获得指定日期的分钟数部分,例如:10:04:15.250 =》 4:"+DateUtil.minute(date));
System.out.println("获得当前年份:"+DateUtil.thisYear());
System.out.println("获得今天是这周的第几天(从周日开始):"+DateUtil.thisDayOfWeek());
System.out.println("获得当前月份(从0开始):"+DateUtil.thisMonth());
System.out.println("获得指定日期是所在年份的第几周(一周从周日开始):"+DateUtil.weekOfYear(date));
//获得月份枚举
Month month1 = DateUtil.monthEnum(date);
//不支持的月份(例如UNDECIMBER)返回-1
System.out.println("月枚举获取当前月(从0开始):"+month1.getValue());
System.out.println("月枚举获取当前月(月份值,对应Month,从1开始计数):"+month1.getValueBaseOne());
System.out.println("月枚举获取本月最后一天(是否闰年):"+month1.getLastDay(false));
}
}
class 根据日期时间计算其他{
public static void main(String[] args) {
/**
* 1秒=1000毫秒
* 1ms (毫秒) 1毫秒=0.001秒=10^-3秒 (millisecond)
* 1μs (微秒) 1微秒=0.000001=10^-6秒 (microsecond)
* 1ns (纳秒) 1纳秒=0.0000000001秒=10^-9秒 (nanosecond)
*/
StopWatch stopWatch = DateUtil.createStopWatch();
// 任务1
stopWatch.start("任务一");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
stopWatch.stop();
// 任务2
stopWatch.start("任务二");
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
stopWatch.stop();
System.out.println("星座:"+DateUtil.getZodiac(Month.FEBRUARY.getValue(), 9));// 水瓶座
System.out.println("属相:"+DateUtil.getChineseZodiac(1994));// 狗
System.out.println("计算指定生日在某年的年龄(周岁):"+DateUtil.age(DateUtil.parse("1994-08-12"),DateUtil.parse("2023-08-11")));
System.out.println("计算法定年龄(周岁):"+DateUtil.ageOfNow("1994-08-12"));
//开始时间
DateTime beginTime = DateUtil.parse("2023-07-31 12:12:12");
//结束时间
DateTime endTime = DateUtil.parse("2023-08-07 12:12:11");
System.out.println("日期差值(绝对值):"+DateUtil.between(beginTime,endTime, DateUnit.HOUR));
System.out.println("日期差值天(true为忽略时分秒):"+DateUtil.betweenDay(beginTime,endTime, true));
System.out.println("日期差值月(true为忽略天):"+DateUtil.betweenMonth(beginTime,endTime, false));
System.out.println("日期差值周(true为忽略时分秒):"+DateUtil.betweenWeek(beginTime,endTime, false));
System.out.println("格式化日期间隔输出,精确到毫秒(XX天XX小时XX分XX秒):"+DateUtil.formatBetween(beginTime,endTime));
System.out.println("日期比较(小于0,等于0,大于0):"+DateUtil.compare(beginTime,endTime));
System.out.println("计算代码块用时(ns秒):"+stopWatch.prettyPrint());
System.out.println("纳秒转毫秒:"+DateUtil.nanosToMillis(10000000000000L));
System.out.println("纳秒转秒,保留小数:"+DateUtil.nanosToSeconds(10000000000000L));
}
}
class 判断{
public static void main(String[] args) {
Date date = DateUtil.date();
//开始时间
DateTime beginTime = DateUtil.parse("2023-07-31 12:12:12");
//结束时间
DateTime endTime = DateUtil.parse("2023-08-07 12:12:11");
System.out.println("是否为上午:"+DateUtil.isAM(date));
System.out.println("是否为下午:"+DateUtil.isPM(date));
System.out.println("当前日期是否在日期指定范围内:"+DateUtil.isIn(date,beginTime,endTime));
System.out.println("是否为本月最后一天:"+DateUtil.isLastDayOfMonth(date));
System.out.println("是否闰年:"+DateUtil.isLeapYear(2024));
System.out.println("检查两个时间段是否有时间重叠:"+DateUtil.isOverlap(beginTime,date,beginTime,endTime));
System.out.println("检查两个时间段是否有时间重叠:"+DateUtil.isOverlap(date,date,beginTime,endTime));
System.out.println("比较两个日期是否为同一天:"+DateUtil.isSameDay(beginTime,date));
System.out.println("比较两个日期是否为同一月:"+DateUtil.isSameMonth(beginTime,date));
System.out.println("是否为相同时间:"+DateUtil.isSameTime(beginTime,date));
System.out.println("比较两个日期是否为同一周(是否为周一,国内第一天为星期一,国外第一天为星期日):"+DateUtil.isSameWeek(beginTime,endTime,true));
System.out.println("是否为周末(周六或周日):"+DateUtil.isWeekend(date));
}
}
class 日期偏移{
public static void main(String[] args) {
//开始时间
DateTime beginTime = DateUtil.parse("2023-07-31 12:12:12");
//获取指定日期偏移指定时间后的时间,生成的偏移日期不影响原日期,
// date - 基准日期
//dateField - 偏移的粒度大小(小时、天、月等)DateField
//offset - 偏移量,正数为向后偏移,负数为向前偏移
System.out.println("获取指定日期偏移指定时间后的时间:"+DateUtil.offset(beginTime,DateField.HOUR,2));
System.out.println("日期偏移天:"+DateUtil.offsetDay(beginTime,2));
System.out.println("日期偏移小时:"+DateUtil.offsetHour(beginTime,-2));
System.out.println("日期偏移月:"+DateUtil.offsetMonth(beginTime,3));
System.out.println("日期偏移分钟:"+DateUtil.offsetMinute(beginTime,2));
System.out.println("日期偏移毫秒:"+DateUtil.offsetMillisecond(beginTime,2000));
System.out.println("日期偏移秒:"+DateUtil.offsetSecond(beginTime,2));
System.out.println("日期偏移周:"+DateUtil.offsetWeek(beginTime,1));
}
}