hutool使用

简介: hutool使用

一.工作常用

### 1.引入依赖

<dependency>

 <groupId>cn.hutool</groupId>

 <artifactId>hutool-core</artifactId>

 <version>5.6.5</version>

</dependency>

2.获取天所在周月

publicstaticvoidmain(String[] args) {

       // 获取当前日期时间

       DateTimenow=DateUtil.date();

       // 获取当前日期所在的周数

       intweekOfYear=DateUtil.weekOfYear(now);

       // 获取当前日期所在的月份,并格式化为两位数字

       Stringmonth=String.format("%02d", DateUtil.month(now) +1);

       // 将周数和月份拼接成字符串

       Stringresult=StrUtil.format("当前日期所在的周数:{},当前日期所在的月份:{}", weekOfYear, month);

       System.out.println(result);

   }

二.时间 DateUtil

1.truncate

修改日期为某个时间字段起始时间

StringdateStr2="2020-02-29 12:59:34";

Datedate2=DateUtil.parse(dateStr2);

finalDateTimedateTime=DateUtil.truncate(date2, DateField.MINUTE);

Assert.assertEquals("2020-02-29 12:59:00", dateTime.toString());

2.round

修改日期为某个时间字段四舍五入时间

String dateStr2 = "2020-02-29 12:59:34";

Date date2 = DateUtil.parse(dateStr2);

DateTime dateTime = DateUtil.round(date2, DateField.MINUTE);

Assert.assertEquals("2020-02-29 12:59:59", dateTime.toString());


dateStr2 = "2020-02-29 12:05:29";

date2 = DateUtil.parse(dateStr2);

dateTime = DateUtil.round(date2, DateField.MINUTE);

Assert.assertEquals("2020-02-29 12:05:00", dateTime.toString());

3.ceiling

修改日期为某个时间字段结束时间

String dateStr2 = "2020-02-29 12:59:34";

Date date2 = DateUtil.parse(dateStr2);

DateTime dateTime = DateUtil.ceiling(date2, DateField.MINUTE);

Assert.assertEquals("2020-02-29 12:59:59", dateTime.toString());

4.beginOfSecond

获取秒级别的开始时间,即忽略毫秒部分

@Test

public void test04() {

   String dateStr2 = "2021-05-16 22:50:34.111";

   Date date2 = DateUtil.parse(dateStr2);

   DateTime dateTime = DateUtil.beginOfSecond(date2);

   Assert.assertEquals("2021-05-16 22:50:34", dateTime.toString());

}

5.endOfSecond

获取秒级别的结束时间,即毫秒设置为 999

@Test

public void test05() {

   String dateStr2 = "2021-05-16 22:50:34.111";

   DateTime date2 = DateUtil.parse(dateStr2, "yyyy-MM-dd HH:mm:ss.SSS");

   int millisecond = DateUtil.millisecond(date2);

   System.out.println(millisecond);

   DateTime dateTime = DateUtil.endOfSecond(date2);

   Assert.assertEquals(999, DateUtil.millisecond(dateTime));

}

6.beginOfHour

获取某小时的开始时间

@Test

public void test06() {

   String dateStr2 = "2021-05-16 22:50:34.111";

   DateTime date2 = DateUtil.parse(dateStr2, "yyyy-MM-dd HH:mm:ss.SSS");

   DateTime dateTime = DateUtil.beginOfHour(date2);

   System.out.println(dateTime);

   Assert.assertEquals("2021-05-16 22:00:00", dateTime.toString());

}

7.endOfHour

获取某小时的结束时间

@Test

public void test07() {

 String dateStr2 = "2021-05-16 22:50:34.111";

 DateTime date2 = DateUtil.parse(dateStr2, "yyyy-MM-dd HH:mm:ss.SSS");

 DateTime dateTime = DateUtil.endOfHour(date2);

 Assert.assertEquals("2021-05-16 22:59:59", dateTime.toString());

}

8.beginOfMinute

获取某分钟的开始时间

@Test

public void test08() {

   String dateStr2 = "2021-05-16 22:50:34.111";

   DateTime date2 = DateUtil.parse(dateStr2, "yyyy-MM-dd HH:mm:ss.SSS");

   DateTime dateTime = DateUtil.beginOfMinute(date2);

   Assert.assertEquals("2021-05-16 22:50:00", dateTime.toString());

}

9.endOfMinute

获取某分钟的结束时间

@Test

public void test09() {

   String dateStr2 = "2021-05-16 22:50:34.111";

   DateTime date2 = DateUtil.parse(dateStr2, "yyyy-MM-dd HH:mm:ss.SSS");

   DateTime dateTime = DateUtil.endOfMinute(date2);

   Assert.assertEquals("2021-05-16 22:50:59", dateTime.toString());

}

10.beginOfDay

获取某天的开始时间

@Test

public void test10() {

 String dateStr = "2017-03-01 00:33:23";

 Date date = DateUtil.parse(dateStr);


 // 一天的开始

 Date beginOfDay = DateUtil.beginOfDay(date);

 Assert.assertEquals("2017-03-01 00:00:00", beginOfDay.toString());

 // 一天的结束

 Date endOfDay = DateUtil.endOfDay(date);

 Assert.assertEquals("2017-03-01 23:59:59", endOfDay.toString());

}

11.endOfDay

获取某天的结束时间

@Test

public void test11() {

 String dateStr = "2017-03-01 00:33:23";

 Date date = DateUtil.parse(dateStr);


 // 一天的开始

 Date beginOfDay = DateUtil.beginOfDay(date);

 Assert.assertEquals("2017-03-01 00:00:00", beginOfDay.toString());

 // 一天的结束

 Date endOfDay = DateUtil.endOfDay(date);

 Assert.assertEquals("2017-03-01 23:59:59", endOfDay.toString());

}

12.beginOfWeek

获取某周的开始时间,周一定为一周的开始时间

@Test

public void test12() {

   String dateStr = "2017-03-01 22:33:23";

   DateTime date = DateUtil.parse(dateStr);

   Objects.requireNonNull(date).setFirstDayOfWeek(Week.MONDAY);


   // 一周的开始

   Date beginOfWeek = DateUtil.beginOfWeek(date);

   Assert.assertEquals("2017-02-27 00:00:00", beginOfWeek.toString());

   // 一周的结束

   Date endOfWeek = DateUtil.endOfWeek(date);

   Assert.assertEquals("2017-03-05 23:59:59", endOfWeek.toString());


   Calendar calendar = DateUtil.calendar(date);

   // 一周的开始

   Calendar begin = DateUtil.beginOfWeek(calendar);

   Assert.assertEquals("2017-02-27 00:00:00", DateUtil.date(begin).toString());

   // 一周的结束

   Calendar end = DateUtil.endOfWeek(calendar);

   Assert.assertEquals("2017-03-05 23:59:59", DateUtil.date(end).toString());

}

cn.hutool.core.date.DateUtil.beginOfWeek(java.util.Date, boolean)

isMondayAsFirstDay 是否周一做为一周的第一天(false 表示周日做为第一天)

@Test

public void test13() {

 String beginStr = "2020-03-11";

 DateTime date = DateUtil.parseDate(beginStr);

 Calendar calendar = date.toCalendar();

 final Calendar begin = DateUtil.beginOfWeek(calendar, false);

 Assert.assertEquals("2020-03-08 00:00:00", DateUtil.date(begin).toString());


 Calendar calendar2 = date.toCalendar();

 final Calendar end = DateUtil.endOfWeek(calendar2, false);

 Assert.assertEquals("2020-03-14 23:59:59", DateUtil.date(end).toString());

}

13.endOfWeek

获取某周的结束时间,周日定为一周的结束

@Test

public void test14() {

 String dateStr = "2017-03-01 22:33:23";

 DateTime date = DateUtil.parse(dateStr);

 Objects.requireNonNull(date).setFirstDayOfWeek(Week.MONDAY);


 // 一周的开始

 Date beginOfWeek = DateUtil.beginOfWeek(date);

 Assert.assertEquals("2017-02-27 00:00:00", beginOfWeek.toString());

 // 一周的结束

 Date endOfWeek = DateUtil.endOfWeek(date);

 Assert.assertEquals("2017-03-05 23:59:59", endOfWeek.toString());


 Calendar calendar = DateUtil.calendar(date);

 // 一周的开始

 Calendar begin = DateUtil.beginOfWeek(calendar);

 Assert.assertEquals("2017-02-27 00:00:00", DateUtil.date(begin).toString());

 // 一周的结束

 Calendar end = DateUtil.endOfWeek(calendar);

 Assert.assertEquals("2017-03-05 23:59:59", DateUtil.date(end).toString());

}

cn.hutool.core.date.DateUtil.endOfWeek(java.util.Date, boolean)

isSundayAsLastDay 是否周日做为一周的最后一天(false 表示周六做为最后一天)

@Test

public void test15() {

 String beginStr = "2020-03-11";

 DateTime date = DateUtil.parseDate(beginStr);

 Calendar calendar = date.toCalendar();

 final Calendar begin = DateUtil.beginOfWeek(calendar, false);

 Assert.assertEquals("2020-03-08 00:00:00", DateUtil.date(begin).toString());


 Calendar calendar2 = date.toCalendar();

 final Calendar end = DateUtil.endOfWeek(calendar2, false);

 Assert.assertEquals("2020-03-14 23:59:59", DateUtil.date(end).toString());

}

14.beginOfMonth

获取某月的开始时间

@Test

public void test16() {

 String dateStr2 = "2021-05-16 22:50:34.111";

 DateTime date2 = DateUtil.parse(dateStr2, "yyyy-MM-dd HH:mm:ss.SSS");

 DateTime dateTime = DateUtil.beginOfMonth(date2);

 Assert.assertEquals("2021-05-01 00:00:00", dateTime.toString());

}

15.endOfMonth

获取某月的结束时间

@Test

public void test17() {

 String dateStr2 = "2021-05-16 22:50:34.111";

 DateTime date2 = DateUtil.parse(dateStr2, "yyyy-MM-dd HH:mm:ss.SSS");

 DateTime dateTime = DateUtil.endOfMonth(date2);

 Assert.assertEquals("2021-05-31 23:59:59", dateTime.toString());

}

16.beginOfQuarter

获取某季度的开始时间

@Test

   public void test18() {

       DateTime dateTime = new DateTime("2021-05-16 23:34:23", DatePattern.NORM_DATETIME_FORMAT);

       // 精确到毫秒

       DateTime beginTime = new DateTime("2021-04-01 00:00:00.000", DatePattern.NORM_DATETIME_MS_FORMAT);

       dateTime = DateUtil.beginOfQuarter(dateTime);

       Assert.assertEquals(beginTime, dateTime);

   }

17.endOfQuarter

获取某季度的结束时间

@Test

   public void test19() {

       Date date = DateUtil.endOfQuarter(

               DateUtil.parse("2020-05-31 00:00:00"));

       Assert.assertEquals("2020-06-30 23:59:59", DateUtil.format(date, "yyyy-MM-dd HH:mm:ss"));

   }

18.beginOfYear

获取某年的开始时间

@Test

public void test20() {

   DateTime date = DateUtil.date();

   date.setField(DateField.YEAR, 2019);

   DateTime endOfYear = DateUtil.beginOfYear(date);

   Assert.assertEquals("2019-01-01 00:00:00", endOfYear.toString());

}

19.endOfYear

获取某年的结束时间

@Test

public void test21() {

   DateTime date = DateUtil.date();

   date.setField(DateField.YEAR, 2019);

   DateTime endOfYear = DateUtil.endOfYear(date);

   Assert.assertEquals("2019-12-31 23:59:59", endOfYear.toString());

}

20.yesterday

昨天

@Test

public void test22() {

   //昨天

   DateTime dateTime = DateUtil.yesterday();

   System.out.println(dateTime);

   Assert.assertNotNull(dateTime);

}

21.tomorrow

明天这个时间点

@Test

public void test23() {

   //明天

   DateTime dateTime = DateUtil.tomorrow();

   System.out.println(dateTime);

   Assert.assertNotNull(dateTime);

}

22.lastWeek

上周这个时间点

@Test

public void test24() {

   //上周

   DateTime dateTime = DateUtil.lastWeek();

   System.out.println(dateTime);

   Assert.assertNotNull(dateTime);

}

23.nextWeek

下周这个时间点

@Test

public void test25() {

 //下周

 DateTime dateTime = DateUtil.nextWeek();

 System.out.println(dateTime);

 Assert.assertNotNull(dateTime);

}

24.lastMonth

上个月

@Test

public void test26() {

   //上个月

   DateTime dateTime = DateUtil.lastMonth();

   System.out.println(dateTime);

   Assert.assertNotNull(dateTime);

}

25.nextMonth

下个月

@Test

public void test27() {

 //下个月

 DateTime dateTime = DateUtil.nextMonth();

 System.out.println(dateTime);

 Assert.assertNotNull(dateTime);

}

26.offsetMillisecond

偏移毫秒数

@Test

public void test28() {

 //偏移毫秒数

 String dateStr2 = "2021-05-16 22:50:34.111";

 DateTime date2 = DateUtil.parse(dateStr2);

 DateTime dateTime = DateUtil.offsetMillisecond(date2, 1);

 int millisecond = DateUtil.millisecond(dateTime);

 Assert.assertEquals(112, millisecond);

 dateTime = DateUtil.offsetMillisecond(date2, -1);

 millisecond = DateUtil.millisecond(dateTime);

 Assert.assertEquals(110, millisecond);

}

27.offsetSecond

偏移秒数

@Test

public void test29() {

 //偏移秒数

 String dateStr2 = "2021-05-16 22:50:34.111";

 DateTime date2 = DateUtil.parse(dateStr2);

 DateTime dateTime = DateUtil.offsetSecond(date2, 1);

 int second = DateUtil.second(dateTime);

 Assert.assertEquals(35, second);

 dateTime = DateUtil.offsetSecond(date2, -1);

 second = DateUtil.second(dateTime);

 Assert.assertEquals(33, second);

}

28.offsetMinute

偏移分钟

@Test

public void test30() {

 //偏移分钟

 String dateStr2 = "2021-05-16 22:50:34.111";

 DateTime date2 = DateUtil.parse(dateStr2);

 DateTime dateTime = DateUtil.offsetMinute(date2, 1);

 int minute = DateUtil.minute(dateTime);

 Assert.assertEquals(51, minute);

 dateTime = DateUtil.offsetMinute(date2, -1);

 minute = DateUtil.minute(dateTime);

 Assert.assertEquals(49, minute);

}

29.offsetHour

偏移小时

@Test

public void test31() {

 //偏移分钟

 String dateStr2 = "2021-05-16 22:50:34.111";

 DateTime date2 = DateUtil.parse(dateStr2);

 DateTime dateTime = DateUtil.offsetHour(date2, 1);

 int hour = DateUtil.hour(dateTime, true);

 Assert.assertEquals(23, hour);

 dateTime = DateUtil.offsetHour(date2, -1);

 hour = DateUtil.hour(dateTime, true);

 Assert.assertEquals(21, hour);

}

30.offsetDay

偏移天

@Test

public void test32() {

   //偏移天

   String dateStr2 = "2021-05-16 22:50:34";

   DateTime date2 = DateUtil.parse(dateStr2);

   DateTime dateTime = DateUtil.offsetDay(date2, 1);

   Assert.assertEquals("2021-05-17 22:50:34", dateTime.toString());

   dateTime = DateUtil.offsetDay(date2, -1);

   Assert.assertEquals("2021-05-15 22:50:34", dateTime.toString());

}

31.offsetWeek

偏移周

@Test

public void test33() {

 //偏移周

 String dateStr2 = "2021-05-16 22:50:34";

 DateTime date2 = DateUtil.parse(dateStr2);

 DateTime dateTime = DateUtil.offsetWeek(date2,1);

 Assert.assertEquals("2021-05-23 22:50:34", dateTime.toString());

 dateTime = DateUtil.offsetWeek(date2,-1);

 Assert.assertEquals("2021-05-09 22:50:34", dateTime.toString());

}

32.offsetMonth

偏移月

@Test

public void test34() {

 //偏移月

 String dateStr2 = "2021-05-16 22:50:34";

 DateTime date2 = DateUtil.parse(dateStr2);

 DateTime dateTime = DateUtil.offsetMonth(date2, 1);

 Assert.assertEquals("2021-06-16 22:50:34", dateTime.toString());

 dateTime = DateUtil.offsetMonth(date2, -1);

 Assert.assertEquals("2021-04-16 22:50:34", dateTime.toString());

}

33.offset

获取指定日期偏移指定时间后的时间,生成的偏移日期不影响原日期

@Test

public void test35() {

 String dateStr = "2017-03-01 22:33:23";

 Date date = DateUtil.parse(dateStr);


 Date newDate = DateUtil.offset(date, DateField.DAY_OF_MONTH, 2);

 Assert.assertEquals("2017-03-03 22:33:23", newDate.toString());

}

三.http 请求

<dependency>

   <groupId>cn.hutool</groupId>

   <artifactId>hutool-all</artifactId>

   <version>5.4.1</version>

</dependency>

1.get 请求

@Value("${command-server.command-host-url}")

private String url;


@SneakyThrows

@Override

public List<TopSkuInfoDTO> getTopSkuInfo() {

   List<TopSkuInfoDTO> topSkuInfoDTOS = new ArrayList<>();

   String realUrl = url + "/open-api/v1/sku/replenish/getTopSkuInfo";

   HttpResponse response = HttpRequest.get(realUrl)

           .header("User-Agent", "Mozilla/5.0")

           .header("Accept-Language", "en-US,en;q=0.5")

           .timeout(5000)

           .execute();

   if (response.isOk()) {

       String result = response.body();

       // 创建ObjectMapper对象

       ObjectMapper mapper = new ObjectMapper();

       // 将JSON字符串转换成Payload对象

       Payload payload = mapper.readValue(result, Payload.class);

       topSkuInfoDTOS = (List<TopSkuInfoDTO>) payload.getPayload();

   }

   return topSkuInfoDTOS;

}

2.get 带参数

@SneakyThrows

@Override

public String getKeyByCode(String productCode) {

   String realUrl = url + "/open-api/v1/sku/replenish/getKeyByCode?productCode=" + productCode;

   HttpResponse response = HttpRequest.get(realUrl)

           .header("User-Agent", "Mozilla/5.0")

           .header("Accept-Language", "en-US,en;q=0.5")

           .timeout(5000)

           .execute();

   if (response.isOk()) {

       String result = response.body();

       // 创建ObjectMapper对象

       ObjectMapper mapper = new ObjectMapper();

       // 将JSON字符串转换成Payload对象

       Payload payload = mapper.readValue(result, Payload.class);

       return (String) payload.getPayload();

   }

   return null;

}

3.post 请求

@SneakyThrows

@Override

publicStringgetUrl(StringproductCode, StringproductKey) {

 StringrealUrl=url+"/open-api/v1/cargoOffice/product/productDetails";

 ProductDetailsQueryquery=newProductDetailsQuery();

 query.setEmployeeCode("xxx");

 query.setApplication("xxx");

 query.setProductKey(productKey);

 query.setProductCode(productCode);

 query.setTimestamp(System.currentTimeMillis());

 query.setBrandDetailNo("xxx");

 query.setSecretId(this.secret);

 Stringsecret=query.getEmployeeCode() +"cargoOffice"+query.getTimestamp() +this.secret;

 StringsecretStr=MD5Util.stringToMd5(secret);

 query.setSecret(secretStr);

 // 创建ObjectMapper对象

 ObjectMappermapper=newObjectMapper();

 StringjsonStr=mapper.writeValueAsString(query);

 HttpResponseresponse=HttpRequest.post(realUrl)

         .header("Content-Type", "application/json")

         .body(jsonStr)

         .execute();

 if (response.isOk()) {

     Stringresult=response.body();

     // 将JSON字符串转换成Payload对象

     Payloadpayload=mapper.readValue(result, Payload.class);

     finalMapmap= (Map) payload.getPayload();

     return (String) map.get("url");

 }

 returnnull;

}


相关文章
|
Prometheus 监控 Cloud Native
DataSophon手动部署Doris操作指南
DataSophon手动部署Doris操作指南
3288 0
|
11月前
|
Oracle Java 应用服务中间件
apache-tomcat-6.0.10使用步骤详解(附启动与部署教程)
Tomcat 6.0.10是经典Java Web服务器,适用于老项目部署。需先安装JDK 6/7并配置环境变量,解压后运行bin目录下startup.bat启动服务,浏览器访问localhost:8080验证。支持war包自动部署,可修改server.xml调整端口,关闭用shutdown.bat或Ctrl+C。
|
6月前
|
人工智能 自然语言处理 安全
阿里云百炼大模型平台详解:定位、模型矩阵、落地场景与计费方案
2026年6月正值阿里云“AI普惠计划”关键期,新用户可免费领取7000万Tokens、首月Pro版免费,企业用户享高额迁移补贴,是体验与落地百炼平台的最佳时机。未来,随着模型能力的持续迭代、应用生态的不断丰富、安全体系的日益完善,阿里云百炼将进一步渗透千行百业,成为企业与用户使用AI大模型的“标配平台”,助力中国AI产业规模化、普惠化发展。
2101 1
|
Java 关系型数据库 MySQL
springboot项目集成dolphinscheduler调度器 实现datax数据同步任务
springboot项目集成dolphinscheduler调度器 实现datax数据同步任务
1283 2
|
内存技术
nvm的使用
nvm的使用
|
存储 SQL 自然语言处理
ClickHouse查询执行与优化
本文详细介绍了SQL语法扩展、执行计划分析及优化策略,涵盖特殊函数与子句(如`WITH`、`ANY JOIN`)、聚合函数扩展(如`uniqCombined`、`quantileTDigest`)以及执行计划优化技巧。同时深入解析了ClickHouse的索引原理,包括主键索引和跳数索引的工作机制与优化方法。针对查询优化,文章提供了过滤条件下推、分布式查询优化和数据预聚合等策略,并探讨了资源管理与并发控制的核心参数(如`max_memory_usage`、`max_threads`)及队列优先级调度机制,助力高效使用ClickHouse。
2052 9
|
开发框架 JavaScript 小程序
uni-app——如何阻止事件冒泡
uni-app——如何阻止事件冒泡
|
资源调度 JavaScript
Vue2拖拽插件(vuedraggable)
这篇文章介绍了如何在Vue 3框架中使用`vuedraggable`插件来实现拖拽功能,并提供了插件的安装、配置和事件处理的示例。
2066 1
Vue2拖拽插件(vuedraggable)
|
编解码 监控 网络协议
如何使用FFmpeg实现RTSP推送H.264和H.265(HEVC)编码视频
本文详细介绍了如何使用FFmpeg实现RTSP推送H.264和H.265(HEVC)编码视频。内容涵盖环境搭建、编码配置、服务器端与客户端实现等方面,适合视频监控系统和直播平台等应用场景。通过具体命令和示例代码,帮助读者快速上手并实现目标。
4582 6