说明:
最近写了一些求时间段的数据,发现若是在mysql写的话有点冗余不通用。然后,参考其他博主和自己改造一下生成一篇通用的日期区间工具类。
注意,针对于跨年的问题。大家使用的时候检验一下是否符合你们的标准,其中周的开始和结束时间可以根据自己的设定来传参控制前几周的数据。但是季度以下代码还不能动态获取前几个季度的数据,大家要注意哦!

代码:

package com.crm.untils;
import java.text.SimpleDateFormat;
import java.util.*;

import static com.jdlims.util.DateUtil.getNowMonth;
import static com.jdlims.util.DateUtil.getNowYear;

/**
 * @PROJECT_NAME: jdlims
 * @DESCRIPTION:
 * @USER: dell
 * @DATE: 2022/4/13 15:16
 */
public class CalendarUtils {
    // 当天的开始时间
    public static Date getDayBegin() {
        Calendar calendar = new GregorianCalendar();
        setMinTime(calendar);
        return calendar.getTime();
    }

    // 当天的结束时间
    public static Date getDayEnd() {
        Calendar calendar = new GregorianCalendar();
        setMaxTime(calendar);
        return calendar.getTime();
    }

    // 昨天的开始时间
    public static Date getBeginDayOfYesterday() {
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(getDayBegin());
        calendar.add(Calendar.DAY_OF_MONTH, -1);
        return calendar.getTime();
    }

    // 昨天的结束时间
    public static Date getEndDayOfYesterDay() {
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(getDayEnd());
        calendar.add(Calendar.DAY_OF_MONTH, -1);
        return calendar.getTime();
    }

    // 前天的开始时间
    public static Date getBeginDayOfBeforeYesterday() {
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(getDayBegin());
        calendar.add(Calendar.DAY_OF_MONTH, -2);
        return calendar.getTime();
    }

    // 前天的结束时间
    public static Date getEndDayOfBeforeYesterDay() {
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(getDayEnd());
        calendar.add(Calendar.DAY_OF_MONTH, -2);
        return calendar.getTime();
    }

    // 明天的开始时间
    public static Date getBeginDayOfTomorrow() {
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(getDayBegin());
        calendar.add(Calendar.DAY_OF_MONTH, 1);
        return calendar.getTime();
    }

    // 明天的结束时间
    public static Date getEndDayOfTomorrow() {
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(getDayEnd());
        calendar.add(Calendar.DAY_OF_MONTH, 1);
        return calendar.getTime();
    }

    // 获得本周一0点时间
    public static Date getBeginDayOfWeek(){
        Calendar calendar = Calendar.getInstance();
        calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONDAY), calendar.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
        calendar.setFirstDayOfWeek(Calendar.MONDAY);
        int dayofweek = calendar.get(Calendar.DAY_OF_WEEK);
        if (dayofweek == 1) {
            dayofweek += 7;
        }
        calendar.add(Calendar.DATE, 2 - dayofweek);
        return calendar.getTime();
    }

    // 获得本周日24点时间
    public static Date getEndDayOfWeek(){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(getBeginDayOfWeek());
        calendar.add(Calendar.DAY_OF_WEEK, 7);
        return calendar.getTime();
    }

    // 获取上周的开始时间
    public static Date getBeginDayOfLastWeek() {
        Calendar calendar = Calendar.getInstance();
        calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONDAY), calendar.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
        calendar.setFirstDayOfWeek(Calendar.MONDAY);
        int dayofweek = calendar.get(Calendar.DAY_OF_WEEK);
        if (dayofweek == 1) {
            dayofweek += 7;
        }
        calendar.add(Calendar.DATE, 2 - dayofweek - 7);
        return calendar.getTime();
    }

    // 获取上周的结束时间
    public static Date getEndDayOfLastWeek(){
        Calendar calendar = Calendar.getInstance();
        calendar.setFirstDayOfWeek(Calendar.MONDAY);
        calendar.setTime(getBeginDayOfLastWeek());
        calendar.add(Calendar.DAY_OF_WEEK, 7);
        return calendar.getTime();
    }

    // 获取上上周的开始时间
    public static Date getBeginDayOfBeforeWeek() {
        Calendar calendar = Calendar.getInstance();
        calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONDAY), calendar.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
        calendar.setFirstDayOfWeek(Calendar.MONDAY);
        int dayofweek = calendar.get(Calendar.DAY_OF_WEEK);
        if (dayofweek == 1) {
            dayofweek += 7;
        }
        calendar.add(Calendar.DATE, 2 - dayofweek - 7 - 7);
        return calendar.getTime();
    }

    // 获取上上周的结束时间
    public static Date getEndDayOfBeforeWeek(){
        Calendar calendar = Calendar.getInstance();
        calendar.setFirstDayOfWeek(Calendar.MONDAY);
        calendar.setTime(getBeginDayOfBeforeWeek());
        calendar.add(Calendar.DAY_OF_WEEK-1, 7);
        return calendar.getTime();
    }

    // 获取本月的开始时间
    public static Date getBeginDayOfMonth() {
        Calendar calendar = Calendar.getInstance();
        calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONDAY), calendar.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
        calendar.set(getNowYear(), getNowMonth() - 1, 1);
        return calendar.getTime();
    }

    // 获取本月的结束时间
    public static Date getEndDayOfMonth() {
        Calendar calendar = Calendar.getInstance();
        calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONDAY), calendar.get(Calendar.DAY_OF_MONTH), 24, 0, 0);
        calendar.set(getNowYear(), getNowMonth() - 1, 1);
        int day = calendar.getActualMaximum(5);
        calendar.set(getNowYear(), getNowMonth() - 1, day);
        return calendar.getTime();
    }

    // 获取上月的开始时间
    public static Date getBeginDayOfLastMonth() {
        Calendar calendar = Calendar.getInstance();
        calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONDAY), calendar.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
        calendar.set(getNowYear(), getNowMonth() - 2, 1);
        return calendar.getTime();
    }

    // 获取上月的结束时间
    public static Date getEndDayOfLastMonth() {
        Calendar calendar = Calendar.getInstance();
        calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONDAY), calendar.get(Calendar.DAY_OF_MONTH), 24, 0, 0);
        calendar.set(getNowYear(), getNowMonth() - 2, 1);
        int day = calendar.getActualMaximum(5);
        calendar.set(getNowYear(), getNowMonth() - 2, day);
        return calendar.getTime();
    }

    // 获取上上月的开始时间
    public static Date getBeginDayOfBeforeMonth() {
        Calendar calendar = Calendar.getInstance();
        calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONDAY), calendar.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
        calendar.set(getNowYear(), getNowMonth() - 3, 1);
        return calendar.getTime();
    }

    // 获取上上月的结束时间
    public static Date getEndDayOfBeforeMonth() {
        Calendar calendar = Calendar.getInstance();
        calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONDAY), calendar.get(Calendar.DAY_OF_MONTH), 24, 0, 0);
        calendar.set(getNowYear(), getNowMonth() - 3, 1);
        int day = calendar.getActualMaximum(5);
        calendar.set(getNowYear(), getNowMonth() - 3, day);
        return calendar.getTime();
    }

    // 当前季度的开始时间
    public static Date getCurrentQuarterStartTime() {
        Calendar c = Calendar.getInstance();
        int currentMonth = c.get(Calendar.MONTH) + 1;
        SimpleDateFormat longSdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        SimpleDateFormat shortSdf = new SimpleDateFormat("yyyy-MM-dd");
        Date now = null;
        try {
            if (currentMonth >= 1 && currentMonth <= 3)
                c.set(Calendar.MONTH, 0);
            else if (currentMonth >= 4 && currentMonth <= 6)
                c.set(Calendar.MONTH, 3);
            else if (currentMonth >= 7 && currentMonth <= 9)
                c.set(Calendar.MONTH, 4);
            else if (currentMonth >= 10 && currentMonth <= 12)
                c.set(Calendar.MONTH, 9);
            c.set(Calendar.DATE, 1);
            now = longSdf.parse(shortSdf.format(c.getTime()) + " 00:00:00");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return now;
    }

    // 当前季度的结束时间
    public static Date getCurrentQuarterEndTime() {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(getCurrentQuarterStartTime());
        calendar.add(Calendar.MONTH, 3);
        return calendar.getTime();
    }

    // 上个季度的开始时间
    public static Date getLastCurrentQuarterStartTime(){
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.MONTH, ((int) calendar.get(Calendar.MONTH) / 3 - 1) * 3);
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        setMinTime(calendar);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTime();
    }

    // 上个季度的结束时间
    public static Date getLastCurrentQuarterEndTime(){
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.MONTH, ((int) calendar.get(Calendar.MONTH) / 3 - 1) * 3 + 3);
        calendar.set(Calendar.DAY_OF_MONTH,1);
        setMinTime(calendar);
        return calendar.getTime();
    }

    // 上上个季度的开始时间
    public static Date getLastCurrentQuarterBeforeStartTime(){
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.MONTH, ((int) calendar.get(Calendar.MONTH) / 6 - 1) * 3);
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        setMinTime(calendar);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTime();
    }

    // 上上个季度的结束时间
    public static Date getLastCurrentQuarterBeforeEndTime(){
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.MONTH, ((int) calendar.get(Calendar.MONTH) / 6 - 1) * 3 + 3);
        calendar.set(Calendar.DAY_OF_MONTH,1);
        setMinTime(calendar);
        return calendar.getTime();
    }

    // 获取本年的开始时间
    public static Date getBeginDayOfYear() {
        Calendar calendar = Calendar.getInstance();
        calendar.set(getNowYear(), Calendar.JANUARY, 1, 0, 0, 0);
        return calendar.getTime();
    }

    // 获取本年的结束时间
    public static java.util.Date getEndDayOfYear() {
        Calendar calendar = Calendar.getInstance();
        calendar.set(getNowYear()+1, Calendar.JANUARY, 1, 0, 0, 0);
        return calendar.getTime();
    }

    // 获取去年的开始时间
    public static Date getBeginDayOfLastYear() {
        Calendar calendar = Calendar.getInstance();
        calendar.set(getNowYear()-1, Calendar.JANUARY, 1, 0, 0, 0);
        return calendar.getTime();
    }

    // 获取去年的结束时间
    public static Date getEndDayOfLastYear() {
        Calendar calendar = Calendar.getInstance();
        calendar.set(getNowYear(), Calendar.JANUARY, 1, 0, 0, 0);
        return calendar.getTime();
    }

    // 获取上上年的开始时间
    public static Date getBeginDayOfBeforeYear() {
        Calendar calendar = Calendar.getInstance();
        calendar.set(getNowYear()-2, Calendar.JANUARY, 1, 0, 0, 0);
        return calendar.getTime();
    }

    // 获取上上年的结束时间
    public static Date getEndDayOfBeforeYear() {
        Calendar calendar = Calendar.getInstance();
        calendar.set(getNowYear()-1, Calendar.JANUARY, 1, 0, 0, 0);
        return calendar.getTime();
    }

    // 结束时间  时分秒:23:59:59
    private static void setMaxTime(Calendar calendar){
        calendar.set(Calendar.HOUR_OF_DAY, calendar.getActualMaximum(Calendar.HOUR_OF_DAY));
        calendar.set(Calendar.MINUTE, calendar.getActualMaximum(Calendar.MINUTE));
        calendar.set(Calendar.SECOND, calendar.getActualMaximum(Calendar.SECOND));
        calendar.set(Calendar.MILLISECOND, calendar.getActualMaximum(Calendar.MILLISECOND));
    }

    // 开始时间 时分秒:00:00:00
    private static void setMinTime(Calendar calendar){
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
    }
}

参考1:https://www.cnblogs.com/whzhaochao/p/5023427.html
参考2:https://blog.csdn.net/cjw1231/article/details/81541952