JAVA 获取今天、昨天、上周等日期

58 篇文章 0 订阅
   /**
     * 获取今日开始时间和结束时间
     *
     * @return 返回 Pair 对象 left 是开始时间 right 是结束时间,类型为Date
     */
    public static Pair<Date,Date> getDayRange() {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.MILLISECOND, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        Date startTime=calendar.getTime();
        calendar.set(Calendar.HOUR_OF_DAY, 24);
        calendar.set(Calendar.SECOND, -1);
        Date stopTime=calendar.getTime();
        return Pair.of(startTime,stopTime);
    }

    /**
     * 获取昨天开始时间和结束时间
     *
     * @return 返回 Pair 对象 left 是开始时间 right 是结束时间,类型为Date
     */
    public static Pair<Date,Date> getYesterdayRange() {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.MILLISECOND, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.HOUR_OF_DAY, -24);
        Date startTime=calendar.getTime();
        calendar.set(Calendar.MILLISECOND, 0);
        calendar.set(Calendar.SECOND, 59);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        Date stopTime=calendar.getTime();
        return Pair.of(startTime,stopTime);
    }

    /**
     * 获得最近三天的开始时间和结束时间
     *
     * @return 返回 Pair 对象 left 是开始时间 right 是结束时间,类型为Date
     */
    public static Pair<Date,Date> getThreeRange() {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.MILLISECOND, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.HOUR_OF_DAY, 24);
        Date stopTime=calendar.getTime();
        calendar.set(Calendar.HOUR_OF_DAY, -72);
        Date startTime=calendar.getTime();
        return Pair.of(startTime,stopTime);
    }

    /**
     * 获得近一周的开始时间和结束时间
     *
     * @return 返回 Pair 对象 left 是开始时间 right 是结束时间,类型为Date
     */
    public static Pair<Date,Date> getDaySevenRange() {
        Calendar calendar = Calendar.getInstance();
        calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
        calendar.set(Calendar.HOUR_OF_DAY, 24);
        Date stopTime=calendar.getTime();
        calendar.set(Calendar.HOUR_OF_DAY, -168);
        Date startTime=calendar.getTime();
        return Pair.of(startTime,stopTime);
    }

    /**
     * 获得近一月的开始时间和结束时间
     *
     * @return 返回 Pair 对象 left 是开始时间 right 是结束时间,类型为Date
     */
    public static Pair<Date,Date> getDayTRange() {
        Calendar calendar = Calendar.getInstance();
        calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
        calendar.set(Calendar.HOUR_OF_DAY, 24);
        Date stopTime=calendar.getTime();
        calendar.set(Calendar.HOUR_OF_DAY, -720);
        Date startTime=calendar.getTime();
        return Pair.of(startTime,stopTime);
    }

    /**
     * 获取本周开始时间和结束时间
     *
     * @return 返回 Pair 对象 left 是开始时间 right 是结束时间,类型为Date
     */
    public static Pair<Date,Date> getWeekRange() {
        Calendar calendar = Calendar.getInstance();
        calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), 0, 0, 0);

        calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
        Date startTime=calendar.getTime();
        calendar.add(Calendar.DAY_OF_WEEK, 7);
        calendar.set(Calendar.SECOND, -1);
        Date stopTime=calendar.getTime();
        return Pair.of(startTime,stopTime);
    }

    /**
     * 获取上周开始时间和结束时间
     *
     * @return 返回 Pair 对象 left 是开始时间 right 是结束时间,类型为Date
     */
    public static Pair<Date,Date> getLastWeekRange() {
        Calendar calendar = Calendar.getInstance();
        calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
        calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
        calendar.add(Calendar.DAY_OF_WEEK, -7);
        Date startTime=calendar.getTime();
        calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), 23, 59, 59);
        calendar.add(Calendar.DAY_OF_WEEK, 6);
        Date stopTime=calendar.getTime();
        return Pair.of(startTime,stopTime);
    }

    /**
     * 获取本月开始时间和结束时间
     *
     * @return 返回 Pair 对象 left 是开始时间 right 是结束时间,类型为Date
     */
    public static Pair<Date,Date> getMonthRange() {
        Calendar calendar = Calendar.getInstance();
        calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMinimum(Calendar.DAY_OF_MONTH));
        Date startTime=calendar.getTime();
        calendar.add(Calendar.DAY_OF_WEEK, 7);
        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
        // calendar.set(Calendar.HOUR_OF_DAY, 24);
        calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), 23, 59, 59);
        Date stopTime=calendar.getTime();
        return Pair.of(startTime,stopTime);
    }

    /**
     * 获取上月开始时间和结束时间
     *
     * @return 返回 Pair 对象 left 是开始时间 right 是结束时间,类型为Date
     */
    public static Pair<Date,Date> getLastMonthRange() {
        Calendar calendar = Calendar.getInstance();
        calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
        calendar.add(Calendar.MONTH, -1);
        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMinimum(Calendar.DAY_OF_MONTH));
        Date startTime=calendar.getTime();
        calendar.add(Calendar.DAY_OF_WEEK, 7);
        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
        //calendar.set(Calendar.HOUR_OF_DAY, 24);
        calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), 23, 59, 59);
        Date stopTime=calendar.getTime();
        return Pair.of(startTime,stopTime);
    }

    /**
     * 获取本季度开始时间和结束时间
     *
     * @return 返回 Pair 对象 left 是开始时间 right 是结束时间,类型为Date
     */
    public static Pair<Date,Date> getQuarterRange() {
        Calendar calendar = Calendar.getInstance();
        calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
        int currentMonth = calendar.get(Calendar.MONTH) + 1;
        try {
            if (currentMonth >= 1 && currentMonth <= 3)
                calendar.set(Calendar.MONTH, 0);
            else if (currentMonth >= 4 && currentMonth <= 6)
                calendar.set(Calendar.MONTH, 3);
            else if (currentMonth >= 7 && currentMonth <= 9)
                calendar.set(Calendar.MONTH, 6);
            else if (currentMonth >= 10 && currentMonth <= 12)
                calendar.set(Calendar.MONTH, 9);
            calendar.set(Calendar.DATE, 1);
        } catch (Exception e) {
            e.printStackTrace();
        }
        Date startTime=calendar.getTime();
        calendar.add(Calendar.MONTH, 3);
        calendar.set(Calendar.SECOND, -1);
        Date stopTime=calendar.getTime();
        return Pair.of(startTime,stopTime);
    }

    /**
     * 获取上季度开始时间和结束时间
     *
     * @return 返回 Pair 对象 left 是开始时间 right 是结束时间,类型为Date
     */
    public static Pair<Date,Date> getLastQuarterRange() {
        Calendar calendar = Calendar.getInstance();
        calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
        int currentMonth = calendar.get(Calendar.MONTH) + 1;
        try {
            if (currentMonth >= 1 && currentMonth <= 3)
                calendar.set(Calendar.MONTH, 0);
            else if (currentMonth >= 4 && currentMonth <= 6)
                calendar.set(Calendar.MONTH, 3);
            else if (currentMonth >= 7 && currentMonth <= 9)
                calendar.set(Calendar.MONTH, 6);
            else if (currentMonth >= 10 && currentMonth <= 12)
                calendar.set(Calendar.MONTH, 9);
            calendar.set(Calendar.DATE, 1);
        } catch (Exception e) {
            e.printStackTrace();
        }
        calendar.add(Calendar.MONTH, -3);
        Date startTime=calendar.getTime();
        calendar.add(Calendar.MONTH, 3);
        calendar.set(Calendar.SECOND, -1);
        Date stopTime=calendar.getTime();
        return Pair.of(startTime,stopTime);
    }

    /**
     * 获取近半年开始时间和结束时间
     *
     * @return 返回 Pair 对象 left 是开始时间 right 是结束时间,类型为Date
     */
    public static Pair<Date,Date> getHalfYearRange() {
        Calendar calendar = Calendar.getInstance();
        calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMinimum(Calendar.DAY_OF_MONTH));
        calendar.add(Calendar.MONTH, -6);
        Date startTime=calendar.getTime();
        calendar.add(Calendar.MONTH, 6);
        calendar.set(Calendar.SECOND, -1);
        Date stopTime=calendar.getTime();
        return Pair.of(startTime,stopTime);
    }

    /**
     * 获取上半年开始时间和结束时间
     *
     * @return 返回 Pair 对象 left 是开始时间 right 是结束时间,类型为Date
     */
    public static Pair<Date,Date> getUpHalfYearRange() {
        Calendar calendar = Calendar.getInstance();
        calendar.set(calendar.get(Calendar.YEAR), 0, calendar.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMinimum(Calendar.DAY_OF_MONTH));
        Date startTime=calendar.getTime();
        calendar.add(Calendar.MONTH, 6);
        calendar.set(Calendar.SECOND, -1);
        Date stopTime=calendar.getTime();
        return Pair.of(startTime,stopTime);
    }

    /**
     * 获取下半年开始时间和结束时间
     *
     * @return 返回 Pair 对象 left 是开始时间 right 是结束时间,类型为Date
     */
    public static Pair<Date,Date> getNextHalfYearRange() {
        Calendar calendar = Calendar.getInstance();
        calendar.set(calendar.get(Calendar.YEAR), 0, calendar.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMinimum(Calendar.DAY_OF_MONTH));
        calendar.add(Calendar.MONTH, 6);
        Date startTime=calendar.getTime();
        calendar.add(Calendar.MONTH, 6);
        calendar.set(Calendar.SECOND, -1);
        Date stopTime=calendar.getTime();
        return Pair.of(startTime,stopTime);
    }


    /**
     * 获取本年开始时间和结束时间
     *
     * @return 返回 Pair 对象 left 是开始时间 right 是结束时间,类型为Date
     */
    public static Pair<Date,Date> getYearRange() {
        Calendar calendar = Calendar.getInstance();
        calendar.set(calendar.get(Calendar.YEAR), 0, calendar.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMinimum(Calendar.DAY_OF_MONTH));
        Date startTime=calendar.getTime();
        calendar.add(Calendar.YEAR, 1);
        calendar.set(Calendar.SECOND, -1);
        Date stopTime=calendar.getTime();
        return Pair.of(startTime,stopTime);
    }

    /**
     * 获取上一年开始时间和结束时间
     *
     * @return 返回 Pair 对象 left 是开始时间 right 是结束时间,类型为Date
     */
    public static Pair<Date,Date> getLastYearRange() {
        Calendar calendar = Calendar.getInstance();
        calendar.set(calendar.get(Calendar.YEAR), 0, calendar.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMinimum(Calendar.DAY_OF_MONTH));
        calendar.add(Calendar.YEAR, -1);
        Date startTime=calendar.getTime();
        calendar.add(Calendar.YEAR, 1);
        calendar.set(Calendar.SECOND, -1);
        Date stopTime=calendar.getTime();
        return Pair.of(startTime,stopTime);
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值