按半年分割整年时间
// 写死的
public static void main(String[] args) throws ParseException {
ArrayList times = new ArrayList<String>();
times.add("2024-01-01_2024-06-30","2024-07-01_2024-12-31") ;
System.out.println(times.toString());
}
按照一定的规则分割时间
/**
* 将时间段按照半年分割
* @param startTime XXXX-01-01
* @param endTime XXXX-12-31
* @param beginDateList
* @param endDateList
*/
//
public static void splitToHalfOfYears(String startTime, String endTime,
List<String> beginDateList, List<String> endDateList) {
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
try {
Date startDate = dateFormat.parse(startTime);
Date endDate = dateFormat.parse(endTime);
Calendar calendar = Calendar.getInstance();
calendar.setTime(startDate);
beginDateList.add( dateFormat.format(startDate));
calendar.add(Calendar.MONTH, 6);
calendar.set(Calendar.DAY_OF_MONTH, 1);
calendar.add(Calendar.DATE, -1);
calendar.set(Calendar.HOUR_OF_DAY, 23);
calendar.set(Calendar.MINUTE, 59);
calendar.set(Calendar.SECOND, 59);
if (calendar.getTimeInMillis() > endDate.getTime()) {
endDateList.add(dateFormat.format(endDate.getTime()));
} else {
endDateList.add(dateFormat.format(calendar.getTimeInMillis()));
while (calendar.getTimeInMillis() < endDate.getTime()) {
calendar.add(Calendar.DATE, 1);
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
beginDateList.add(dateFormat.format(calendar.getTimeInMillis()));
calendar.add(Calendar.MONTH, 6);
calendar.set(Calendar.DAY_OF_MONTH, 1);
calendar.add(Calendar.DATE, -1);
calendar.set(Calendar.HOUR_OF_DAY, 23);
calendar.set(Calendar.MINUTE, 59);
calendar.set(Calendar.SECOND, 59);
if (calendar.getTimeInMillis() < endDate.getTime()) {
endDateList.add(dateFormat.format(calendar.getTimeInMillis()));
} else {
endDateList.add(dateFormat.format(endDate.getTime()));
}
}
}
} catch (Exception e) {
System.out.println(e.toString());
// logger.info("月计划生成错误", e);
}
}
public static void main(String[] args) throws ParseException {
List<String> beginDateList = new ArrayList<>();
List<String> endDateList = new ArrayList<>();
List<String> timeList = new ArrayList<>();
splitToHalfOfYears("2023-01-01","2023-12-31", beginDateList, endDateList);
System.out.println(beginDateList.toString());
System.out.println(endDateList.toString());
for (int i = 0; i < beginOfList.size(); i++) {
String stat = beginOfList.get(i);
String end = endOfList.get(i);
timeList.add(stat+"_"+end);
}
System.out.println(timeList.toString());
}
运行结果:
时间按旬分割
/**
* 根据日期获取月初和月末
* @param firstDay
* @return
*/
public static List<String> firstMonthAndEndMonth(String firstDay) {
List<String> monthList = new ArrayList<>();
try {
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
Date startTime = dateFormat.parse(firstDay);
Calendar cal = Calendar.getInstance();
cal.setTime(startTime); // 将Date类型转换为Calendar类型
cal.set(Calendar.DAY_OF_MONTH, 1);
String stat = dateFormat.format(cal.getTime());
cal.set(Calendar.MONTH, cal.get(Calendar.MONTH) + 1);
cal.set(Calendar.DAY_OF_MONTH, 1);
cal.set(Calendar.HOUR_OF_DAY, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
Date lastDayOfMonth = new Date(cal.getTimeInMillis() - 1);
String end = dateFormat.format(lastDayOfMonth);
monthList.add(stat +"_"+ end);
} catch (Exception e ) {
e.printStackTrace();
}
return monthList;
}
/**
* 按旬分割日期
* @param cycleNum
* @param startTime
* @param endTime
* @return
*/
public static List<String> getTendayCycleList(int cycleNum, String startTime, String endTime){
List<String> beginDateList = new ArrayList<>();
List<String> endDateList = new ArrayList<>();
getIntervalTimeByMonth(startTime,endTime,beginDateList,endDateList);
List<String> datList = new ArrayList<>();
try{
for (int i = 0; i < beginDateList.size(); i++) {
List<String> cycleList = getCycleList(cycleNum, beginDateList.get(i), endDateList.get(i));
for (int j = 0; j < cycleList.size(); j++) {
datList.add(cycleList.get(j));
}
}
}catch (Exception e) {
e.printStackTrace();
}
return datList;
}
/**
* 按天分割
* @param cycleNum
* @param startTime
* @param endTime
* @return
* @throws ParseException
* @throws ParseException
*/
public static List<String> getCycleList(int cycleNum, String startTime, String endTime) throws ParseException, ParseException {
logger.info("统计的时间段为:从"+startTime+"到"+endTime);
Date endParse = new SimpleDateFormat("yyyy-MM-dd").parse(endTime);
Long endLong = endParse.getTime();
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
// endTime = sdf.format(new Date(endLong));
logger.info("结束时间是:"+endTime);
int daysBetweenNum = daysBetween(startTime, endTime);
int cycleForNum = daysBetweenNum % cycleNum == 0 ? (daysBetweenNum / cycleNum) : (daysBetweenNum / cycleNum) + 1;
logger.info("两日期间相隔的天数为:" + daysBetweenNum);
logger.info("周期选择是:" + cycleNum + "天一周期, 则切割出来的周期存在个数:" + cycleForNum);
Date startParse = new SimpleDateFormat("yyyy-MM-dd").parse(startTime);
Long startLong = startParse.getTime();
Long calculationLong = startLong;
List<String> cycleTimeList=new ArrayList();
for (int i = 1; i <= cycleForNum; i++) {
String startStr = sdf.format(new Date(calculationLong));
Date startStrParse = new SimpleDateFormat("yyyy-MM-dd").parse(startStr);
Long startStrLong = startStrParse.getTime();
Long cycleLong = cycleNum* 86400000L;
calculationLong = startStrLong + cycleLong;
String endStr = endTime;
if (calculationLong <= endLong && i<cycleForNum) {
Long calculationLong1 = calculationLong - 86400000L;
endStr = sdf.format(new Date(calculationLong1));
}
if (i == cycleForNum) {
endStr = endTime;
}
logger.info("起始:" + startStr + " 结束:" + endStr);
cycleTimeList.add(startStr +"_"+ endStr);
}
return cycleTimeList;
}
public static int daysBetween(String smdate, String bdate) throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
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);
System.out.println("between_days"+between_days);
return Integer.parseInt(String.valueOf(between_days));
}
/**
* 按照月份分割一段时间
*
* @param startTime 开始时间戳(毫秒)
* @param endTime 结束时间戳(毫秒)
* @param beginDateList 开始段时间戳 和 结束段时间戳 一一对应
* @param endDateList 结束段时间戳 和 开始段时间戳 一一对应
*/
public static void getIntervalTimeByMonth(String startTime, String endTime,
List<String> beginDateList, List<String> endDateList) {
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
DateFormat newdateFormat = new SimpleDateFormat("yyyy-MM-dd");
try {
Date startDate = dateFormat.parse(startTime);
Date endDate = dateFormat.parse(endTime);
Calendar calendar = Calendar.getInstance();
calendar.setTime(startDate);
String time = newdateFormat.format(calendar.getTimeInMillis());
beginDateList.add(time);
while (calendar.getTimeInMillis() < endDate.getTime()) {
calendar.add(Calendar.MONTH, 1);
calendar.set(Calendar.DAY_OF_MONTH, 1);
calendar.add(Calendar.DATE, -1);
calendar.set(Calendar.HOUR_OF_DAY, 23);
calendar.set(Calendar.MINUTE, 59);
calendar.set(Calendar.SECOND, 59);
if(calendar.getTimeInMillis() < endDate.getTime()){
endDateList.add( newdateFormat.format(calendar.getTimeInMillis()));
} else {
endDateList.add(newdateFormat.format(endDate.getTime()));
break;
}
calendar.add(Calendar.DATE, 1);
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
beginDateList.add( newdateFormat.format(calendar.getTimeInMillis()));
}
} catch (Exception e ){
System.out.println(e.toString());
// logger.info("月计划生成错误", e);
}
}
public static void main(String[] args) throws ParseException {
List<String> strings = firstMonthAndEndMonth("2024-12-01");
System.out.println(strings.toString());
String monthS = strings.get(0);
String[] split = monthS.split("_");
List<String> tendayCycleList = getTendayCycleList(10, split[0], split[1]);
System.out.println(tendayCycleList.toString());
}