JAVA將一段時間 按周、月、季度、半年、年進行分割

郭慕荣發表於2024-09-30

JAVA將一段時間 按周、月、季度、半年、年進行分割。
有時候開發常常會傳入一個開始時間和結束時間,將兩個時間進行分割,返回一個時間的集合以下是我們小組成員所書寫的實體類

package com.aisino.utils;
import java.text.SimpleDateFormat;
import java.util.*;

public class SpiltDateUtil {
    private static SimpleDateFormat monthDateFormat = new SimpleDateFormat("MM");
    //將時間段按星期分割
    public static final List<Range> splitToWeeks(Date start, Date end) {
        List<Range> result = new ArrayList<>();
        result.add(Range.create(start));
        Date from = new Date(start.getTime() + 7L * 24 * 3600 * 1000);
        Date weekEnd = cn.hutool.core.date.DateUtil.endOfWeek(end);
        while (from.compareTo(weekEnd) <= 0) {
            Date dt = cn.hutool.core.date.DateUtil.beginOfWeek(from);
            CollectionUtil.last(result).end(new Date(dt.getTime() - 24L * 3600 * 1000));
            CollectionUtil.last(result).setMonth(monthDateFormat.format(new Date(dt.getTime() - 24L * 3600 * 1000)));
            result.add(Range.create(dt));
            from.setTime(from.getTime() + 7L * 24 * 3600 * 1000);
        }
        CollectionUtil.last(result).end(end);
        CollectionUtil.last(result).setMonth(monthDateFormat.format(end));
        return result;
    }

    //將時間段按照月分割
    public static final List<Range> splitToMonths(Date start, Date end) {
        List<Range> result = new ArrayList<>();
        result.add(Range.create(start));
        Calendar cal = Calendar.getInstance();
        cal.setTime(start);
        cal.add(Calendar.MONTH, 1);
        Date monthEnd = cn.hutool.core.date.DateUtil.endOfMonth(end);
        while (cal.getTimeInMillis() <= monthEnd.getTime()) {
            Date dt = cn.hutool.core.date.DateUtil.beginOfMonth(cal.getTime());
            CollectionUtil.last(result).end(new Date(dt.getTime() - 24L * 3600 * 1000));
            CollectionUtil.last(result).setMonth(monthDateFormat.format(new Date(dt.getTime() - 24L * 3600 * 1000)));
            result.add(Range.create(dt));
            cal.add(Calendar.MONTH, 1);
        }
        CollectionUtil.last(result).end(end);
        CollectionUtil.last(result).setMonth(monthDateFormat.format(end));
        return result;
    }

    //將時間段按照季度分割
    public static final List<Range> splitToQuarts(Date start, Date end) {
        List<Range> result = new ArrayList<>();
        result.add(Range.create(start));
        Calendar cal = Calendar.getInstance();
        cal.setTime(start);
        cal.add(Calendar.MONTH, 3);
        Date quartEnd = cn.hutool.core.date.DateUtil.endOfQuarter(end);
        while (cal.getTimeInMillis() <= quartEnd.getTime()) {
            Date dt = cn.hutool.core.date.DateUtil.beginOfQuarter(cal.getTime());
            CollectionUtil.last(result).end(new Date(dt.getTime() - 24L * 3600 * 1000));
            System.out.println(cal.get(Calendar.MONTH));
            if (cal.get(Calendar.MONTH) >= 1 && cal.get(Calendar.MONTH) <= 3) {
                CollectionUtil.last(result).setMonth("4");
            } else if (cal.get(Calendar.MONTH) >= 4 && cal.get(Calendar.MONTH) <= 6) {
                CollectionUtil.last(result).setMonth("1");
            } else if (cal.get(Calendar.MONTH) >= 7 && cal.get(Calendar.MONTH) <= 9) {
                CollectionUtil.last(result).setMonth("2");
            } else if (cal.get(Calendar.MONTH) >= 10 && cal.get(Calendar.MONTH) <= 12) {
                CollectionUtil.last(result).setMonth("3");
            }
            result.add(Range.create(dt));
            cal.add(Calendar.MONTH, 3);
        }
        CollectionUtil.last(result).end(end);
        System.out.println(cal.get(Calendar.MONTH));
        if (cal.get(Calendar.MONTH) >= 1 && cal.get(Calendar.MONTH) <= 3) {
            CollectionUtil.last(result).setMonth("4");
        } else if (cal.get(Calendar.MONTH) >= 4 && cal.get(Calendar.MONTH) <= 6) {
            CollectionUtil.last(result).setMonth("1");
        } else if (cal.get(Calendar.MONTH) >= 7 && cal.get(Calendar.MONTH) <= 9) {
            CollectionUtil.last(result).setMonth("2");
        } else if (cal.get(Calendar.MONTH) >= 10 && cal.get(Calendar.MONTH) <= 12) {
            CollectionUtil.last(result).setMonth("3");
        }
        return result;
    }

    //將時間段按照半年分割
    public static final List<Range> splitToHalfOfYears(Date start, Date end) {
        List<Range> result = new ArrayList<>();
        result.add(Range.create(start));
        Calendar cal = Calendar.getInstance();
        cal.setTime(start);
        cal.add(Calendar.MONTH, 6);
        Calendar calOne = Calendar.getInstance();
        calOne.setTime(end);
        int mon = cal.get(Calendar.MONTH);
//        Calendar tmp = Calendar.getInstance();
//        if (mon < 6) {
//            tmp.setTimeInMillis(cal.getTimeInMillis());
//            tmp.set(Calendar.MONTH, 5);
//        } else {
//            tmp.setTimeInMillis(cal.getTimeInMillis());
//            tmp.set(Calendar.MONTH, 11);
//        }
        Calendar halfYearEnd = cn.hutool.core.date.DateUtil.endOfMonth(calOne);
        while (cal.getTimeInMillis() <= halfYearEnd.getTimeInMillis()) {
            mon = cal.get(Calendar.MONTH);
            Calendar cal1 = Calendar.getInstance();
            if (mon < 6) {
                cal1.setTimeInMillis(cal.getTimeInMillis());
                cal1.set(Calendar.MONTH, 0);
            } else {
                cal1.setTimeInMillis(cal.getTimeInMillis());
                cal1.set(Calendar.MONTH, 6);
            }
            Date dt = cn.hutool.core.date.DateUtil.beginOfMonth(cal1.getTime());
            CollectionUtil.last(result).end(new Date(dt.getTime() - 24L * 3600 * 1000));
            result.add(Range.create(dt));
            cal.add(Calendar.MONTH, 6);
        }
        CollectionUtil.last(result).end(end);
        return result;
    }

    //將時間段按照年分割
    public static final List<Range> splitToYears(Date start, Date end) {
        List<Range> result = new ArrayList<>();
        result.add(Range.create(start));
        Calendar cal = Calendar.getInstance();
        cal.setTime(start);
        cal.add(Calendar.YEAR, 1);
        Date yearEnd = cn.hutool.core.date.DateUtil.endOfYear(end);
        while (cal.getTimeInMillis() <= yearEnd.getTime()) {
            Date dt = cn.hutool.core.date.DateUtil.beginOfYear(cal.getTime());
            CollectionUtil.last(result).end(new Date(dt.getTime() - 24L * 3600 * 1000));
            result.add(Range.create(dt));
            cal.add(Calendar.YEAR, 1);
        }
        CollectionUtil.last(result).end(end);
        return result;
    }

    //日期區間
    public static class Range {
        Date start;
        Date end;
        String Month;

        public Range() {

        }

        private Range(Date start) {
            this.start = start;
        }

        public static Range create(Date start) {
            return new Range(start);
        }

        public Range end(Date end) {
            this.end = end;
            return this;
        }

        public void setStart(Date start) {
            this.start = start;
        }

        public void setEnd(Date end) {
            this.end = end;
        }

        public Date getStart() {
            return start;
        }

        public Date getEnd() {
            return end;
        }

        public String getMonth() {
            return Month;
        }

        public void setMonth(String month) {
            Month = month;
        }

        @Override
        public String toString() {
            return "[" + cn.hutool.core.date.DateUtil.format(start, "yyyy-MM-dd") + "," + cn.hutool.core.date.DateUtil.format(end, "yyyy-MM-dd")
                    + "]";
        }
    }

    //根據年,月,第幾周獲取時間範圍
    public static Map getWeekDateLimits(String year, String month, String week) {
        Map result = new HashMap();
        SimpleDateFormat dft = new SimpleDateFormat("yyyy-MM-dd");
        Calendar c1 = Calendar.getInstance();
        c1.set(Calendar.YEAR, Integer.parseInt(year));
        c1.set(Calendar.MONTH, Integer.parseInt(month) - 1);
        c1.set(Calendar.DAY_OF_WEEK, 2);
        c1.set(Calendar.WEEK_OF_MONTH, Integer.parseInt(week));
        Date time1 = c1.getTime();
        String begin = dft.format(time1);
        c1.set(Calendar.DATE, c1.get(Calendar.DATE) + 6);
        Date time2 = c1.getTime();
        String end = dft.format(time2);
        result.put("begin", begin);
        result.put("end", end);
        return result;
    }


    public static void main(String[] args) {
        try {
            Calendar c = Calendar.getInstance();
            c.set(Calendar.YEAR, 2021);
            c.set(Calendar.MONTH, (5 - 1));
            c.setFirstDayOfWeek(Calendar.MONDAY);
            System.out.println( c.get(Calendar.YEAR) + "" + (c.get(Calendar.MONTH) + 1) + "");
            System.out.println("天數:" + c.getActualMaximum(Calendar.DAY_OF_MONTH));
            System.out.println("Actual週數:" + c.getActualMaximum(Calendar.WEEK_OF_MONTH));
            System.out.println("Max週數:" + c.getMaximum(Calendar.WEEK_OF_MONTH));
            for (int i = 0; i < 6; i++) {
                System.out.println(getWeekDateLimits(2021 + "", 5 + "", (i + 1) + ""));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
class CollectionUtil {
//獲取最後一行
public static final <S> S last(List<S> list) {
int size = list.size();
if (size != 0) {
return list.get(size - 1);
}
return null;
}
}
//將時間段按星期分割
   public static final List<Range> splitToWeeks(Date start, Date end) {
       List<Range> result = new ArrayList<>();
       result.add(Range.create(start));
       Date from = new Date(start.getTime() + 7L * 24 * 3600 * 1000);
       Date weekEnd = cn.hutool.core.date.DateUtil.endOfWeek(end);
       while (from.compareTo(weekEnd) <= 0) {
           Date dt = cn.hutool.core.date.DateUtil.beginOfWeek(from);

           CollectionUtil.getLast(result).end(new Date(dt.getTime() - 24L * 3600 * 1000));
           result.add(Range.create(dt));
           from.setTime(from.getTime() + 7L * 24 * 3600 * 1000);
       }
       CollectionUtil.getLast(result).end(end);
       return result;
   }

   //將時間段按照月分割
   public static final List<Range> splitToMonths(Date start, Date end) {
       List<Range> result = new ArrayList<>();
       result.add(Range.create(start));
       Calendar cal = Calendar.getInstance();
       cal.setTime(start);
       cal.add(Calendar.MONTH, 1);
       Date monthEnd = cn.hutool.core.date.DateUtil.endOfMonth(end);
       while (cal.getTimeInMillis() <= monthEnd.getTime()) {
           Date dt = cn.hutool.core.date.DateUtil.beginOfMonth(cal.getTime());
           CollectionUtil.getLast(result).end(new Date(dt.getTime() - 24L * 3600 * 1000));
           result.add(Range.create(dt));
           cal.add(Calendar.MONTH, 1);
       }
       CollectionUtil.getLast(result).end(end);
       return result;
   }

   //將時間段按照季度分割
   public static final List<Range> splitToQuarts(Date start, Date end) {
       List<Range> result = new ArrayList<>();
       result.add(Range.create(start));
       Calendar cal = Calendar.getInstance();
       cal.setTime(start);
       cal.add(Calendar.MONTH, 3);
       Date quartEnd = cn.hutool.core.date.DateUtil.endOfQuarter(end);
       while (cal.getTimeInMillis() <= quartEnd.getTime()) {
           Date dt = cn.hutool.core.date.DateUtil.beginOfQuarter(cal.getTime());
           CollectionUtil.getLast(result).end(new Date(dt.getTime() - 24L * 3600 * 1000));
           result.add(Range.create(dt));
           cal.add(Calendar.MONTH, 3);
       }
       CollectionUtil.getLast(result).end(end);
       return result;
   }

   //將時間段按照半年分割
   public static final List<Range> splitToHalfOfYears(Date start, Date end) {
       List<Range> result = new ArrayList<>();
       result.add(Range.create(start));
       Calendar cal = Calendar.getInstance();
       cal.setTime(start);
       cal.add(Calendar.MONTH, 6);
       int mon = cal.get(Calendar.MONTH);
       int tmpMon = DateUtil.month(end);
       Calendar tmp = Calendar.getInstance();
       if (tmpMon < 6) {
           tmp.setTimeInMillis(cal.getTimeInMillis());
           tmp.set(Calendar.MONTH, 5);
       } else {
           tmp.setTimeInMillis(cal.getTimeInMillis());
           tmp.set(Calendar.MONTH, 11);
       }
       tmp.set(Calendar.YEAR, DateUtil.year(end));
       Calendar halfYearEnd = cn.hutool.core.date.DateUtil.endOfMonth(tmp);
       long timeInMillis = halfYearEnd.getTimeInMillis();
       while (cal.getTimeInMillis() <= timeInMillis) {
           mon = cal.get(Calendar.MONTH);
           Calendar cal1 = Calendar.getInstance();
           if (mon < 6) {
               cal1.setTimeInMillis(cal.getTimeInMillis());
               cal1.set(Calendar.MONTH, 0);
           } else {
               cal1.setTimeInMillis(cal.getTimeInMillis());
               cal1.set(Calendar.MONTH, 6);
           }
           Date dt = cn.hutool.core.date.DateUtil.beginOfMonth(cal1.getTime());
           CollectionUtil.getLast(result).end(new Date(dt.getTime() - 24L * 3600 * 1000));
           result.add(Range.create(dt));
           cal.add(Calendar.MONTH, 6);
       }
       CollectionUtil.getLast(result).end(end);
       return result;
   }

   //將時間段按照年分割
   public static final List<Range> splitToYears(Date start, Date end) {
       List<Range> result = new ArrayList<>();
       result.add(Range.create(start));
       Calendar cal = Calendar.getInstance();
       cal.setTime(start);
       cal.add(Calendar.YEAR, 1);
       Date yearEnd = cn.hutool.core.date.DateUtil.endOfYear(end);
       while (cal.getTimeInMillis() <= yearEnd.getTime()) {
           Date dt = cn.hutool.core.date.DateUtil.beginOfYear(cal.getTime());
           CollectionUtil.getLast(result).end(new Date(dt.getTime() - 24L * 3600 * 1000));
           result.add(Range.create(dt));
           cal.add(Calendar.YEAR, 1);
       }
       CollectionUtil.getLast(result).end(end);
       return result;
   }

   //日期區間
   public static class Range {
       Date start;
       Date end;

       private Range(Date start) {
           this.start = start;
       }

       public static Range create(Date start) {
           return new Range(start);
       }

       public Range end(Date end) {
           this.end = end;
           return this;
       }

       public Date getStart() {
           return start;
       }

       public Date getEnd() {
           return end;
       }

       @Override
       public String toString() {
           return "[" + cn.hutool.core.date.DateUtil.format(start, "yyyy-MM-dd") + "-" + cn.hutool.core.date.DateUtil.format(end, "yyyy-MM-dd")
                   + "]";
       }
   }

彙總:對日期進行分割

相關文章