栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

算法 - 常用工具类整理

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

算法 - 常用工具类整理

常用工具
  • 1.将现有集合按照指定的长度拆分为几个子集合
  • 2.两个时间进行比较

1.将现有集合按照指定的长度拆分为几个子集合

引入依赖包:

 
      org.apache.commons
      commons-collections4
      4.2


      com.google.guava
      guava
      23.0

 public  List> split(List resList,int subListLength) {
      if (CollectionUtils.isEmpty(resList) || subListLength <= 0) {
         return Lists.newArrayList();
      }
      int listLength = resList.size();
      List> ansList = new ArrayList<>();
      if (listLength <= subListLength) {
          ansList.add(resList);
      } else {
          int pre = listLength / subListLength;
          int last = listLength % subListLength;
          for (int i = 0; i < pre;i++) {
              ansList.add(resList.subList(subListLength * i,subListLength * (i + 1)));
          }
          if (last > 0) {
              ansList.add(resList.subList(subListLength * pre,resList.size()));
          }
      }
      return ansList;
    }
2.两个时间进行比较

比较两个时间间间隔几天:

 // compareIntevalDays_1("2021-12-21","2021-12-22"); 打印结果为1
    public void compareIntevalDays_1(String startTime,String endTime) {
        DateTimeFormatter pattern = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        LocalDate startDate = LocalDate.parse(startTime, pattern);
        LocalDate endDate = LocalDate.parse(endTime, pattern);
        Period period = Period.between(startDate, endDate);
        System.out.println(period.getDays());
    }

    // compareIntevalDays_2("2021-12-21 00:00:00","2021-12-22 01:00:00");打印结果为1。
    // 转化为LocalDate后,时分秒均为00:00:00,所以相差天数为1。但是在实际业务中如果两个时间只能间隔1天,此时这两个时间实际间隔1天1个小时,所以需要使用LocalDateTime
    public void compareIntevalDays_2(String startTime,String endTime) {
        DateTimeFormatter pattern = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDate startDate = LocalDate.parse(startTime, pattern);
        LocalDate endDate = LocalDate.parse(endTime, pattern);
        Period period = Period.between(startDate, endDate);
        System.out.println(period.getDays());
    }

    // compareIntevalDays_3("2021-12-21 00:00:00","2021-12-22 01:00:00");打印结果为1。
    public void compareIntevalDays_3(String startTime,String endTime) {
        DateTimeFormatter pattern = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime startDateTime = LocalDateTime.parse(startTime, pattern);
        LocalDateTime endDateTime = LocalDateTime.parse(endTime, pattern);
        Period period = Period.between(startDateTime.toLocalDate(), endDateTime.toLocalDate()); // 这种和compareIntevalDays_2的情况一样
        System.out.println(period.getDays());
        Duration duration = Duration.between(startDateTime, endDateTime);
        long millis = duration.toMillis();//相差毫秒数
        if (millis > 24 * 60 * 60 * 1000) {
            System.out.println("开始创建时间和结束创建时间差不能超过1天"); // 控制台打印此信息
        }
    }

比较两个时间的大小:

// compareTime("2021-12-23 00:00:00","2021-12-22 01:00:00");
    public void compareTime(String startTime,String endTime) {
        DateTimeFormatter pattern = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime startDateTime = LocalDateTime.parse(startTime, pattern);
        LocalDateTime endDateTime = LocalDateTime.parse(endTime, pattern);
        if (startDateTime.compareTo(endDateTime) > 0) {
            System.out.println("开始创建时间不能大于截止创建时间");// 控制台打印此信息
        }
    }
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/696525.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号