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;

}


相关文章
|
算法 Java 数据安全/隐私保护
Java:Hutool工具箱之Hutool-crypto加密解密
Java:Hutool工具箱之Hutool-crypto加密解密
2576 0
Java:Hutool工具箱之Hutool-crypto加密解密
|
7月前
|
JSON 算法 Java
Hutool包常用工具类的使用
Hutool包常用工具类的使用
hutool动态编译+lombok
hutool动态编译+lombok
144 1
|
7月前
|
XML 算法 Java
Hutool该怎么用?
Hutool 是一个中国开发者常用的 Java 工具包,包含了多个模块,如 AOP、BloomFilter、缓存、日期、加密、HTTP 客户端、日志、脚本执行、设置文件处理、系统参数调用等。
135 0
|
7月前
|
JSON fastjson Java
Gson与FastJson详解
综上,Gson和FastJson都是用于Java对象和JSON数据互相转换的优秀库,选择哪个取决于性能、功能需求和个人偏好。 买CN2云服务器,免备案服务器,高防服务器,就选蓝易云。百度搜索:蓝易云
101 2
|
7月前
|
安全 Java
Hutool 笔记 日期工具类的使用
Hutool 笔记 日期工具类的使用
312 0
|
XML 编解码 算法
Hutool超级工具类
Hutool超级工具类
97 0
|
Java 网络安全
Hutool发送邮件
Hutool发送邮件
304 0
|
easyexcel API 数据安全/隐私保护
EasyExcel使用与详细说明,EasyExcel工具类(三)
EasyExcel使用与详细说明,EasyExcel工具类
9365 1
|
存储 JavaScript easyexcel
EasyExcel使用与详细说明,EasyExcel工具类(一)
EasyExcel使用与详细说明,EasyExcel工具类
1293 0