Java 实现将连续的日期 按照天数进行分割

2023-02-13 08:36:42 浏览数 (1)

目录

  • 1 工具类
  • 2 实现

1 工具类

代码语言:javascript复制
    public  static List<Map<String,Object>> getCycleList(int cycleNum, String startTime, String endTime) throws ParseException, ParseException {
        System.out.println("统计的时间段为:从" startTime "到" endTime);
        Date endParse = new SimpleDateFormat(DateUtils.DATE_TIME_FORMAT_YYYY_MM_DD_HH_MI_SS).parse(endTime);
        Long endLong = endParse.getTime();

        SimpleDateFormat sdf=new SimpleDateFormat(DateUtils.DATE_TIME_FORMAT_YYYY_MM_DD_HH_MI_SS);//这个是你要转成后的时间的格式
        endTime = sdf.format(new Date(endLong));
        System.out.println("结束时间是:" endTime);

        int daysBetweenNum = daysBetween(startTime, endTime);
        int cycleForNum = daysBetweenNum % cycleNum == 0 ? (daysBetweenNum / cycleNum) : (daysBetweenNum / cycleNum)   1;
        System.out.println("两日期间相隔的天数为:"   daysBetweenNum);
        System.out.println("周期选择是:"   cycleNum   "天一周期, 则切割出来的周期存在个数:"   cycleForNum);
        Date startParse = new SimpleDateFormat(DateUtils.DATE_TIME_FORMAT_YYYY_MM_DD_HH_MI_SS).parse(startTime);
        Long startLong = startParse.getTime();

        Long calculationLong = startLong;

        List<Map<String,Object>> CycleTimeList=new ArrayList();
        for (int i = 1; i <= cycleForNum; i  ) {

            String startStr = sdf.format(new Date(calculationLong));
            Date startStrParse = new SimpleDateFormat(DateUtils.DATE_TIME_FORMAT_YYYY_MM_DD_HH_MI_SS).parse(startStr);
            Long startStrLong = startStrParse.getTime();
            Long cycleLong = cycleNum* 86400000L;
            calculationLong = startStrLong   cycleLong;
            String endStr = endTime;
            if (calculationLong <= endLong) {
                endStr = sdf.format(new Date(calculationLong));
            }
            System.out.println("起始:"   startStr   "结束:"   endStr);
            Map<String, Object> stringObjectHashMap = new HashMap<>();
            stringObjectHashMap.put("starttime",startStr);
            stringObjectHashMap.put("endtime",endStr);

            CycleTimeList.add(stringObjectHashMap);
        }
        System.out.println("周期list:" CycleTimeList.toString());
        return CycleTimeList;
    }





    public static int daysBetween(String smdate, String bdate) throws ParseException {

        SimpleDateFormat sdf = new SimpleDateFormat(DateUtils.DATE_TIME_FORMAT_YYYY_MM_DD_HH_MI_SS);
        Calendar cal = Calendar.getInstance();
        cal.setTime(sdf.parse(smdate));
        long time1 = cal.getTimeInMillis();
        cal.setTime(sdf.parse(bdate));
        long time2 = cal.getTimeInMillis();
        long between_days = (time2 - time1) / (1000 * 3600 * 24);
        return Integer.parseInt(String.valueOf(between_days));

    }

2 实现

代码语言:javascript复制
        try {
            getCycleList(2,"2023-01-21 18:32:51","2023-01-29 18:32:51");
        } catch (ParseException e) {
            e.printStackTrace();
        }
代码语言:javascript复制
  try {
            getCycleList(1,"2023-01-22 18:32:51","2023-01-29 18:32:51");
        } catch (ParseException e) {
            e.printStackTrace();
        }

0 人点赞