官网地址:https://apidoc.gitee.com/loolly/hutool/
引入依赖:
cn.hutool hutool-core5.6.5 org.projectlombok lombok1.16.10 provided
1、测试案例:
@Test public void test1(){ DateTime date = DateUtil.date(); log.info("当前时间,转换为DateTime对象:"+date); DateTime dateTime = DateUtil.dateSecond(); log.info("当前时间,转换为DateTime对象,忽略毫秒部分:"+dateTime); String now = DateUtil.now(); log.info("当前时间,格式 yyyy-MM-dd HH:mm:ss:"+now); String today = DateUtil.today(); log.info("当前日期,格式 yyyy-MM-dd:"+today); }
2、测试案例:
@Test public void test2(){ Date date = new Date(); DateTime dateTime = DateUtil.date(date); log.info("Date类型时间转为DateTime:"+dateTime); long millis = System.currentTimeMillis(); DateTime date1 = DateUtil.date(millis); log.info("Long类型时间转为DateTime:"+date1); }
3、测试案例:
@Test public void test3(){ Date date = new Date(); int year = DateUtil.year(date); log.info("获得年的部分:"+year); int month = DateUtil.month(date); log.info("获得月份,从0开始计数:"+month); }
4、测试案例:
@Test public void test4(){ DateTime date = DateUtil.date(); log.info("当前时间:"+date); DateTime offsetMillisecond = DateUtil.offsetMillisecond(date, -10); log.info("偏移毫秒数:"+ offsetMillisecond); DateTime offsetMinute = DateUtil.offsetMinute(date, -10); log.info("偏移分钟:"+ offsetMinute); DateTime offsetHour = DateUtil.offsetHour(date, -10); log.info("偏移小时:"+ offsetHour); DateTime offsetDay = DateUtil.offsetDay(date, -1); log.info("偏移天:"+ offsetDay); DateTime offsetWeek = DateUtil.offsetWeek(date, -1); log.info("偏移周:"+ offsetWeek); DateTime offsetMonth = DateUtil.offsetMonth(date, -1); log.info("偏移月:"+ offsetMonth); }
5、测试案例:
@Test public void test5(){ DateTime date = DateUtil.date(); log.info("当前时间:"+date); DateTime beginOfSecond = DateUtil.beginOfSecond(date); log.info("获取秒级别的开始时间,即忽略毫秒部分:"+ beginOfSecond); DateTime endOfSecond = DateUtil.endOfSecond(date); log.info("获取秒级别的结束时间,即毫秒设置为999:"+ endOfSecond); DateTime beginOfMinute = DateUtil.beginOfMinute(date); log.info("获取某分钟的开始时间:" + beginOfMinute); DateTime endOfMinute = DateUtil.endOfMinute(date); log.info("获取某分钟的结束时间:" + endOfMinute); DateTime beginOfHour = DateUtil.beginOfHour(date); log.info("获取某小时的开始时间:" + beginOfHour); DateTime endOfHour = DateUtil.endOfHour(date); log.info("获取某小时的结束时间:" + endOfHour); DateTime beginOfDay = DateUtil.beginOfDay(date); log.info("获取某天的开始时间:"+ beginOfDay); DateTime endOfDay = DateUtil.endOfDay(date); log.info("获取某天的结束时间:"+ endOfDay); DateTime beginOfWeek = DateUtil.beginOfWeek(date); log.info("获取某周的开始时间,周一定为一周的开始时间:" + beginOfWeek); DateTime endOfWeek = DateUtil.endOfWeek(date); log.info("获取某周的结束时间,周日定为一周的结束:" + endOfWeek); DateTime beginOfMonth = DateUtil.beginOfMonth(date); log.info("获取某月的开始时间:" + beginOfMonth); DateTime endOfMonth = DateUtil.endOfMonth(date); log.info("获取某月的结束时间:" + endOfMonth); DateTime beginOfQuarter = DateUtil.beginOfQuarter(date); log.info("获取某季度的开始时间:" + beginOfQuarter); DateTime endOfQuarter = DateUtil.endOfQuarter(date); log.info("获取某季度的结束时间:" + endOfQuarter); DateTime beginOfYear = DateUtil.beginOfYear(date); log.info("获取某年的开始时间:" + beginOfYear); DateTime endOfYear = DateUtil.endOfYear(date); log.info("获取某年的结束时间:" + endOfYear); }
6、测试案例:
@Test public void test6(){ Date date = new Date(); log.info("未格式化之前:" + date); String formatDate = DateUtil.formatDate(date); log.info("格式化日期部分(不包括时间),格式 yyyy-MM-dd:" + formatDate); String formatDateTime = DateUtil.formatDateTime(date); log.info("格式化日期时间,格式 yyyy-MM-dd HH:mm:ss:" + formatDateTime); String formatTime = DateUtil.formatTime(date); log.info("格式化时间,格式 HH:mm:ss:"+ formatTime); DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String format = DateUtil.format(date, dateFormat); log.info("根据特定格式格式化日期:" + format); }
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)