首页 > 编程语言 >JAVA将一段时间 按周、月、季度、半年、年进行分割

JAVA将一段时间 按周、月、季度、半年、年进行分割

时间:2024-09-30 09:34:38浏览次数:11  
标签:分割 JAVA start MONTH 按周 cal Date end Calendar

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")
                   + "]";
       }
   }

汇总:对日期进行分割

 

标签:分割,JAVA,start,MONTH,按周,cal,Date,end,Calendar
From: https://www.cnblogs.com/jelly12345/p/18441212

相关文章

  • 第二周(java的多态性)
    1、类的多态:属性不会被覆盖,当父类和子类中存在相同的属性名时,子类中会有两个,具体是哪个值,看声明的类名!!             方法会被覆盖,编译看左边,执行看右边。2、charc1='A';inti=65;c1==i //true  不论谁先谁后因为char类型会被自动类型转换为in......
  • opencv实战项目二十九:GrabCut分割人像
    提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档文章目录前言一、GrabCut介绍:二、opencv实现:三、效果:前言在数字图像处理领域,人像分割是一项极具挑战性的任务,它旨在从复杂背景中准确提取出人物图像。随着技术的不断发展,人像分割技术在许多领域都展......
  • 初学Java基础Day08 方法,方法的递归,方法的重载
    一,方法1.概念:        特定功能的代码块2.好处:        减少代码的冗余3.分类:1.无参数无返回值的方法2.带参数的方法3.带返回的方法4.理解:        参数是方法调用时传入的数据,返回值是方法执行完毕后返回的数据1.无参数无返回的方法//语法结......
  • 初学Java基础Day09---不死神兔,方法版本的万年历
    一,不死神兔(方法递归版)不死神兔也叫做斐波那契数列或者叫做黄金分割数列或者叫做兔子数列:不死神兔问题:有1对兔子,从出生后的第3个月起每个月都生一对兔子,小兔子长到第三个月后每个月又生一对兔子,假如兔子都不死,问第n个月有几对兔子1.找规律当月兔子的对数等于上个月对数加上上......
  • 【Java】—— 常用类和基础API:JDK8之前日期时间API的使用:Date、SimpleDateFormat、Cal
    目录3.JDK8之前:日期时间API3.1java.lang.System类的方法3.2java.util.Date3.3java.text.SimpleDateFormat3.4java.util.Calendar(日历)练习3.JDK8之前:日期时间API3.1java.lang.System类的方法System类提供的publicstaticlongcurrentTimeMillis():用来返......
  • Java Deeplearning4j:构建和训练卷积神经网络(CNN)模型
    ......
  • JavaScript
    什么是JavaScript?JavaScript(简称:JS)是一门跨平台、面向对象的脚本语言(脚本语言是不需要编译,直接通过浏览器的解释就可以运行)。是用来控制网页行为的,它能使网页可交互。JavaScript和Java是完全不同的语言,不论是概念还是设计。但是基础语法类似。JavaScript在1......
  • Java的原码、反码和补码
    在Java中,原码、反码和补码是用来表示整数的三种不同的编码方式。这三种编码方式分别用来表示正数、负数以及0。原码:原码是整数的最基本的表示方式,它直接将整数的二进制表示出来。原码的表示方法是,最高位表示符号位(0表示正数,1表示负数),其余位表示整数的绝对值的二进制表示。......
  • 计算机毕业设计-基于Java+SSM架构的程序设计课程可视化教学系统项目开发实战(附源码+
    大家好!我是职场程序猿,感谢您阅读本文,欢迎一键三连哦。......
  • Java中流的学习
    流的基本概念目录流的基本概念一、流的基本概念二、流的分类三、流的使用场景四、流的特性Java中关于流(Stream)的知识是一个广泛而深入的主题,它涵盖了Java标准库中用于处理输入输出(IO)的多种抽象和类。以下是对Java流知识的详细归纳:一、流的基本概念定义:Java流是Java标准库中......