根据年份+月度+月获取开始时间和结束时间

接收前端年份、月度、月获取开始时间和结束时间

/**
     * @param year    年为0则是全部,后面的季度和月份就不用选择了
     * @param quarter 季度[0,1,2,3,4],0是全部,剩下的是四个季度,1-3,4-6,7,9,10-12
     * @param month   月份[0-12]
     * @return
     */
    public static List<Date> getDateList(int year, int quarter, int month) {

        List<Date> list = new ArrayList<>(2);
        LocalDateTime d1 = null;
        LocalDateTime d2 = null;

        LocalDateTime localDate = LocalDateTime.now();
        if (year == 0 || (localDate.getYear() != year && localDate.getYear() - 1 != year)) {
            list.add(null);
            list.add(null);
            return list;
        }

        //全年
        if (quarter == 0 && month == 0) {
            d1 = LocalDateTime.of(year, Month.JANUARY, 1, 0, 0);
            d2 = LocalDateTime.of(year, Month.DECEMBER, 31, 23, 59);
        } else {
            switch (quarter) {
                case 1:
                    d1 = LocalDateTime.of(year, Month.JANUARY, 1, 0, 0);
                    d2 = LocalDateTime.of(year, Month.MARCH, 31, 23, 59);
                    break;
                case 2:
                    d1 = LocalDateTime.of(year, Month.APRIL, 1, 0, 0);
                    d2 = LocalDateTime.of(year, Month.JUNE, 30, 23, 59);
                    break;
                case 3:
                    d1 = LocalDateTime.of(year, Month.JULY, 1, 0, 0);
                    d2 = LocalDateTime.of(year, Month.SEPTEMBER, 30, 23, 59);
                    break;
                case 4:
                    d1 = LocalDateTime.of(year, Month.OCTOBER, 1, 0, 0);
                    d2 = LocalDateTime.of(year, Month.DECEMBER, 31, 23, 59);
                    break;
                default:
            }
            if (month != 0) {
                d1 = LocalDateTime.of(year, month, 1, 0, 0);
                d2 = d1.with(TemporalAdjusters.lastDayOfMonth());
            }
        }
        ZoneId zoneId = ZoneId.systemDefault();
        ZonedDateTime zdt1 = d1.atZone(zoneId);
        ZonedDateTime zdt2 = d2.atZone(zoneId);
        Date from = Date.from(zdt1.toInstant());
        Date to = Date.from(zdt2.toInstant());
        list.add(from);
        list.add(to);
        return list;
    }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值