分割时间段工具类

系列文章目录

分割时间工具类(支持秒级分割)



前言

随着数据量的不断扩大,一下子上传一定的时间范围的数据可能会因为数据量过大从而导致程序卡死,因此,分割时间工具类用于减少数据量查询,切割成几批分段上传


一、SplitDateTimeUtils是什么?

示例:SplitDateTimeUtils是基于java 的一种工具,该工具是为了解决数据分割任务而创建的。

二、使用步骤

1.相关代码

package com.stpass.synchronizer.util;

import java.time.Duration;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
//切割时间工具类
public class SplitDateTimeUtils {
    //按天数分割时间段
    public static List<Map<String, LocalDateTime>> splitDateByDay(LocalDateTime startTime, LocalDateTime endTime, long daysum) {
        List<Map<String, LocalDateTime>> dayList = null;
        if (daysum > 0) {
            dayList = new ArrayList<>();
            Duration between = Duration.between(startTime, endTime);
            long betweenDay = between.toDays();
            HashMap<String, LocalDateTime> map = null;
            if (betweenDay > daysum) {
                for (long i = 0; i <= betweenDay; i = i + daysum + 1) {
                    map = new HashMap<>();
                    LocalDateTime startDateTime = LocalDateTime.of(startTime.toLocalDate(), LocalTime.MIN).plusDays(i);
                    LocalDateTime endDateTime = LocalDateTime.of(startDateTime.plusDays(daysum).toLocalDate(), LocalTime.MAX);
                    map.put("startTime", startDateTime);
                    if (Duration.between(endDateTime, endTime).toDays() < 0) {
                        endDateTime = endTime;
                    }
                    map.put("endTime", endDateTime);
                    dayList.add(map);
                }
            } else if (betweenDay < 0) {
                return dayList;
            } else {
                commonMethod(startTime, endTime, dayList);
            }
        }
        return dayList;
    }



    //按小时↑分割时间段
    public static List<Map<String, LocalDateTime>> splitDateByHours(LocalDateTime startTime, LocalDateTime endTime, long hourSum) {
        --hourSum;
        List<Map<String, LocalDateTime>> dayList = null;
        if (hourSum > 0) {
            dayList = new ArrayList<>();
            Duration between = Duration.between(startTime, endTime);
            long betweenHour = between.toHours();
            HashMap<String, LocalDateTime> map = null;
            if (betweenHour > hourSum) {
                for (long i = 0; i <= betweenHour; i = i + hourSum + 1) {
                    map = new HashMap<>();
                    LocalDateTime startDateTime = LocalDateTime.of(startTime.toLocalDate(), LocalTime.MIN).plusHours(i);
                    LocalDateTime endDateTime = LocalDateTime.of(startTime.toLocalDate(), LocalTime.MIN).plusHours(i+hourSum+1).minusNanos(1);
                    map.put("startTime", startDateTime);
                    if (Duration.between(endDateTime, endTime).toDays() < 0) {
                        endDateTime = endTime;
                    }
                    map.put("endTime", endDateTime);
                    dayList.add(map);
                }
            } else if (betweenHour < 0) {
                return dayList;
            } else {
                commonMethod(startTime, endTime, dayList);
            }
        }
        return dayList;
    }

    //按分钟↑分割时间段
    public static List<Map<String, LocalDateTime>> splitDateByMinute(LocalDateTime startTime, LocalDateTime endTime, long minutesum) {
        --minutesum;
        List<Map<String, LocalDateTime>> dayList = null;
        if (minutesum > 0) {
            dayList = new ArrayList<>();
            Duration between = Duration.between(startTime, endTime);
            long minutes = between.toMinutes();
            HashMap<String, LocalDateTime> map = null;
            if (minutes > minutesum) {
                for (long i = 0; i <= minutes; i = i + minutesum + 1) {
                    map = new HashMap<>();
                    LocalDateTime startDateTime = LocalDateTime.of(startTime.toLocalDate(), LocalTime.MIN).plusMinutes(i);
                    LocalDateTime endDateTime = LocalDateTime.of(startTime.toLocalDate(), LocalTime.MIN).plusMinutes(i+minutesum+1).minusNanos(1);
                    map.put("startTime", startDateTime);
                    if (Duration.between(endDateTime, endTime).toMinutes() < 0) {
                        endDateTime = endTime;
                    }
                    map.put("endTime", endDateTime);
                    dayList.add(map);
                }
            } else if (minutes < 0) {
                return dayList;
            } else {
                commonMethod(startTime, endTime, dayList);
            }
        }
        return dayList;
    }

    //按秒↑分割时间段
    public static List<Map<String, LocalDateTime>> splitDateBySecond(LocalDateTime startTime, LocalDateTime endTime, long secondsum) {
        --secondsum;
        List<Map<String, LocalDateTime>> dayList = null;
        if (secondsum > 0) {
            dayList = new ArrayList<>();
            Duration between = Duration.between(startTime, endTime);
            long seconds = between.toMillis()/1000;
            HashMap<String, LocalDateTime> map = null;
            if (seconds > secondsum) {
                for (long i = 0; i <= seconds; i = i + secondsum + 1) {
                    map = new HashMap<>();
                    LocalDateTime startDateTime = LocalDateTime.of(startTime.toLocalDate(), LocalTime.MIN).plusSeconds(i);
                    LocalDateTime endDateTime = LocalDateTime.of(startTime.toLocalDate(), LocalTime.MIN).plusSeconds(i+secondsum+1).minusNanos(1);
                    map.put("startTime", startDateTime);
                    if (Duration.between(endDateTime, endTime).toMillis() < 0) {
                        endDateTime = endTime;
                    }
                    map.put("endTime", endDateTime);
                    dayList.add(map);
                }
            } else if (seconds < 0) {
                return dayList;
            } else {
                commonMethod(startTime, endTime, dayList);
            }
        }
        return dayList;
    }

    private static void commonMethod(LocalDateTime startTime, LocalDateTime endTime, List<Map<String, LocalDateTime>> dayList) {
        HashMap<String, LocalDateTime> map;
        map = new HashMap<>();
        map.put("startTime", startTime);
        map.put("endTime", endTime);
        dayList.add(map);
    }

    public static void main(String[] args) {
        LocalDateTime startTime = LocalDateTime.parse("2020-06-12 00:00:00", DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        LocalDateTime endTime = LocalDateTime.parse("2020-06-12 13:01:58", DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        List<Map<String, LocalDateTime>> mapList = splitDateByHours(startTime, endTime, 2);
        mapList.forEach(e-> System.out.println(e.get("startTime")+"--->"+e.get("endTime")));
    }
}

总结

用于分割时间段
  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值