系列文章目录
分割时间工具类(支持秒级分割)
前言
随着数据量的不断扩大,一下子上传一定的时间范围的数据可能会因为数据量过大从而导致程序卡死,因此,分割时间工具类用于减少数据量查询,切割成几批分段上传
一、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")));
}
}