java日期分割大法

将时间段按 [日, 周, 月] 进行分割.

import java.text.DateFormat;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.*;
import java.text.ParseException;

public class DateUtil {
	static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	public static void main(String[] args){
		Date now = null;
		try {
			now = sdf.parse("2020-12-01 12:34:56");
		}
		catch(ParseException e) {
			System.out.println("日期解析错误"); 
			now = new Date();
		}
		Calendar c = Calendar.getInstance();
		c.add(Calendar.MONTH,12);
		List<Map<String,Date>> list = splitDate(now,c.getTime(),2,3);
		System.out.println(sdf.format(now)+"\t"+sdf.format(c.getTime())+"\t"+list.size()+"\n-----------------开始分割------------------"); 
		list.forEach(it-> System.out.println(sdf.format(it.get("startDate")) + "~" + sdf.format(it.get("endDate"))));
	} 
	
	/**
     * 获取本月最后一天的日期
     * @param date 日期
     * @return 结果
     * @date 2020-12-29
     */
    public static Date getLastDayOfThisMonth(Date date){
        Calendar c = getFirstDayOfMonth(date);
        c.add(Calendar.MONTH, 1);
        c.add(Calendar.MILLISECOND, -1);
        return c.getTime();
    }



    /**
     * 获取本月第一天的日期
     * @param date 日期
     * @return 结果
     * @date 2020-12-29
     */
    public static Date getFirstDayOfThisMonth(Date date){
        return getFirstDayOfMonth(date).getTime();
    }

    /**
     * 获取下月第一天0点0分0秒
     * @param date 一个日期
     * @return 结果日期
     * @date 2020-12-29
     */
    public static Date getFirstDayOfNextMonth(Date date){
        Calendar c = getFirstDayOfMonth(date);
        c.add(Calendar.MONTH, 1);
        return c.getTime();
    }

    /**
     * 获取本月第一天0点0分0秒
     * @param date 日期
     * @return Calendar 日历
     * @date 2020-12-29
     */
    public static Calendar getFirstDayOfMonth(Date date){
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.set(c.get(Calendar.YEAR),c.get(Calendar.MONTH),1,0,0,0);
        c.set(Calendar.MILLISECOND,0);
        return c;
    }

    /**
     * 获取本 <b>周</b> 第一天0点0分0秒
     * @param date 日期
     * @return Date 本周第一天
     * @date 2020-12-29
     */
    public static Date getFirstDayOfThisWeek(Date date){
        Calendar c = getBeginOfThisDay(date);
        c.add(Calendar.WEEK_OF_MONTH, 0);
        c.set(Calendar.DAY_OF_WEEK, 2);
        return c.getTime();
    }

    /**
     * 获取本 <b>周</b> 最后一天23点59分59秒
     * @param date 日期
     * @return Date 本周最后一天
     * @date 2020-12-29
     */
    public static Date getLastDayOfThisWeek(Date date){
        Calendar c = getBeginOfThisDay(date);
        c.set(Calendar.DAY_OF_WEEK, c.getActualMaximum(Calendar.DAY_OF_WEEK));
        c.add(Calendar.DAY_OF_WEEK, 2);
		c.add(Calendar.MILLISECOND,-1);
        return c.getTime();
    }

    /**
     * 获取下 <b>周</b> 第一天0点0分0秒
     * @param date 日期
     * @return Date 下周第一天
     * @date 2020-12-29
     */
    public static Date getFirstDayOfNextWeek(Date date){
        Calendar c = getBeginOfThisDay(date);
        c.set(Calendar.DAY_OF_WEEK, c.getActualMaximum(Calendar.DAY_OF_WEEK));
        c.add(Calendar.DAY_OF_WEEK, 2);
        return c.getTime();
    }

    /**
     * 获取当天0点0分0秒
     * @param date 日期
     * @return Calendar 日历
     * @date 2020-12-29
     */
    public static Calendar getBeginOfThisDay(Date date){
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.set(c.get(Calendar.YEAR),c.get(Calendar.MONTH),c.get(Calendar.DAY_OF_MONTH),0,0,0);
        c.set(Calendar.MILLISECOND,0);
        return c;
    }

    /**
     * 获取当天0点0分0秒
     * @param date 日期
     * @return Calendar 日历
     * @date 2020-12-29
     */
    public static Calendar getEndOfThisDay(Date date){
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.set(c.get(Calendar.YEAR),c.get(Calendar.MONTH),c.get(Calendar.DAY_OF_MONTH),23,59,59);
        c.set(Calendar.MILLISECOND,999);
        return c;
    }

    /**
     * 日期运算
     * @param date base date
     * @param field the calendar field
     * @param amount the amount of date
     * @return calculated date
     */
    public static Date add(Date date, int field, int amount) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(field, amount);
        return calendar.getTime();
    }

    /**
     * 根据条件分割时间段
     * @param startDate 开始时间
     * @param endDate 结束时间
     * @param unit 分割单位
     * @param length 分割长度
     * @return 结果 [{startDate,endDate}]
     */
    public static List<Map<String,Date>> splitDate(Date startDate,Date endDate, Integer unit, Integer length){
        unit = unit==null?2:unit;
        length = length==null || length < 1?1:length;
        List<Map<String,Date>> list = new ArrayList<>();
        switch (unit){
            case 0:
                Calendar beginDay = getBeginOfThisDay(startDate);
                while (beginDay.getTimeInMillis()<endDate.getTime()){
                    Map<String,Date> map = new HashMap<>(2);
                    // 开始日期
                    map.put("startDate", beginDay.getTime());
                    beginDay.add(Calendar.DAY_OF_MONTH, length);
                    // 结束日期
                    beginDay.add(Calendar.SECOND,-1);
                    map.put("endDate",beginDay.getTime());
                    list.add(map);
                    // 增加1s,变成第二天
                    beginDay.add(Calendar.SECOND,1);

                }
                return list;
            case 1:
                Calendar beginWeek = getBeginOfThisDay(startDate);
                int dayOfWeek = beginWeek.get(Calendar.DAY_OF_WEEK);
                if (dayOfWeek!=1){
                    Map<String,Date> firstMap = new HashMap<>(2);
                    // 开始日期
                    firstMap.put("startDate", beginWeek.getTime());
                    Date end = getLastDayOfThisWeek(startDate);
                    firstMap.put("endDate", end);
                    list.add(firstMap);
                    beginWeek = getEndOfThisDay(end);
                    beginWeek.add(Calendar.MILLISECOND,1);
                }
                while (beginWeek.getTimeInMillis()<endDate.getTime()){
                    Map<String,Date> map = new HashMap<>(2);
                    // 开始日期
                    map.put("startDate", beginWeek.getTime());
                    beginWeek.add(Calendar.WEEK_OF_MONTH,length);
                    // 如果增加length周后的时间大于endDate, 跳出
                    if (beginWeek.getTimeInMillis()>endDate.getTime()){
                        break;
                    }
                    beginWeek.add(Calendar.SECOND,-1);
                    map.put("endDate", beginWeek.getTime());
                    list.add(map);
                    beginWeek.add(Calendar.SECOND,1);
                }
                beginWeek.add(Calendar.WEEK_OF_MONTH,-length);
                if (beginWeek.getTimeInMillis()<endDate.getTime()){
                    Map<String,Date> map = new HashMap<>(2);
                    map.put("startDate", beginWeek.getTime());
                    map.put("endDate", endDate);
                    list.add(map);
                }
                return list;
            case 2:
                Calendar beginMonth = getBeginOfThisDay(startDate);
                int dayOfMonth = beginMonth.get(Calendar.DAY_OF_MONTH);
                if (dayOfMonth!=1){
                    Map<String,Date> firstMap = new HashMap<>(2);
                    // 开始日期
                    firstMap.put("startDate", beginMonth.getTime());
                    Date end = getLastDayOfThisMonth(startDate);
                    firstMap.put("endDate", end);
                    list.add(firstMap);
                    beginMonth = getEndOfThisDay(end);
                    beginMonth.add(Calendar.MILLISECOND,1);
                }
                while (beginMonth.getTimeInMillis()<endDate.getTime()){
                    Map<String,Date> map = new HashMap<>(2);
                    // 开始日期
                    map.put("startDate", beginMonth.getTime());
                    beginMonth.add(Calendar.MONTH,length);
                    // 如果增加length周后的时间大于endDate, 跳出
                    if (beginMonth.getTimeInMillis()>endDate.getTime()){
                        break;
                    }
                    beginMonth.add(Calendar.SECOND,-1);
                    map.put("endDate", beginMonth.getTime());
                    list.add(map);
                    beginMonth.add(Calendar.SECOND,1);
                }
                beginMonth.add(Calendar.MONTH,-length);
                if (beginMonth.getTimeInMillis()<endDate.getTime()){
                    Map<String,Date> map = new HashMap<>(2);
                    map.put("startDate", beginMonth.getTime());
                    map.put("endDate", endDate);
                    list.add(map);
                }
                return list;
            default:
                Map<String,Date> map = new HashMap<String,Date>(2){{
                    put("startDate",startDate);
                    put("endDate",endDate);
                }};
                list.add(map);
                return list;
        }
    }
}

写了很多, 大家精简之后再用.
在这里插入图片描述
这是测试结果

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值