Java中输入两个年份日期,输出年份之间每个月起止日期

最近做项目,有个会计区间,里面就涉及到年份区间自动生成每个月的起止日期,若是输入了指定日期,则按照指定日期作为起止日输出。
话不多说上代码

public void addFinancialRangeList(String hospitalId, String creator, String startYear, String endYear, Integer endDay) {
        //将年份截取出来
        String sYear = startYear.substring(0, 4);
        String eYear = endYear.substring(0, 4);
        //计算起始年份间隔
        Integer firstYear = Integer.valueOf(sYear);
        Integer lastYear = Integer.valueOf(eYear);
        int num = 1;
        if (!firstYear.equals(lastYear)) {
            num = lastYear - firstYear;
        }
        //循环判断当前传入的年份是否已添加
        for (int i = 0; i < num; i++) {
            int number = financialRangeBizService.checkYear(hospitalId, firstYear.toString());
            AssertUtil.isLessThan(number, 1, "当前区间年份已存在!!!");
            firstYear++;
        }
        //格式化为年月日
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

        //创建大小两个日期对象
        Calendar min = Calendar.getInstance();
        Calendar max = Calendar.getInstance();
        //将起止年份赋值
        try {
            min.setTime(sdf.parse(startYear));
            max.setTime(sdf.parse(endYear));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        //自然月计算的起止日期
        Integer start = 1;
        Integer end = 31;
        //若是传入了endDay,则以endDay为起止日期
        if (!ValidateUtil.isEmpty(endDay)) {
            start = endDay + 1;
            end = endDay;
            //月份减一
            min.add(Calendar.MONTH, -1);
        }
        //起止时间对象赋值
        min.set(min.get(Calendar.YEAR), min.get(Calendar.MONTH), start);
        max.set(max.get(Calendar.YEAR), max.get(Calendar.MONTH), end);

        Calendar curr = min;
        //时间格式化
        SimpleDateFormat ym = new SimpleDateFormat("yyyyMM");
        //根据起止年份循环赋值
        while (curr.before(max)) {
            FinancialRangeDTO financialRange = new FinancialRangeDTO();
            financialRange.setBeginDate(curr.getTime());
            financialRange.setEndDate(getMonthEnd(curr.getTime(), endDay));
            financialRange.setYearMonth(ym.format(curr.getTime()));
            financialRange.setIsEffective(true);
            financialRange.setCreator(creator);
            financialRange.setHospitalId(hospitalId);
            financialRangeBizService.addFinancialRange(financialRange);
            //月份循环
            curr.add(Calendar.MONTH, 1);
        }
    }
    public static Date getMonthEnd(Date date, Integer endDay) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        //设置为当月最后一天,若是有enDay,则设置为下个月的endDay
        if (ValidateUtil.isEmpty(endDay)){
            c.set(Calendar.DAY_OF_MONTH, c.getActualMaximum(Calendar.DAY_OF_MONTH));
        }else {
            c.add(Calendar.MONTH, 1);
            c.set(Calendar.DAY_OF_MONTH, endDay);
        }
        //将小时至23
        c.set(Calendar.HOUR_OF_DAY, 23);
        //将分钟至59
        c.set(Calendar.MINUTE, 59);
        //将秒至59
        c.set(Calendar.SECOND, 59);
        //将毫秒至999
        c.set(Calendar.MILLISECOND, 999);
        // 获取本月最后一天的时间
        return c.getTime();
    }

数据库存入结果如图所示:
若为指定日期,则结果是从上个月的指定日期开始计算

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值