java实现两日期间的日期输出和月份输出

本文介绍了一种方法,用于生成两个给定日期之间的所有日期和月份。通过使用Java编程语言,提供了具体的代码实现,包括如何获取指定日期范围内的每一天和每个月份。

输入开始日期和结束日期,输出中间的日期和月份。如2017-01-01和2018-03-05,
输入日期中间日期为:

2017-01-012017-01-02,2017-01-03......2018-03-05

输出中间月份为:

2017-012017-02,2017-03......2018-03

计算中间日期代码:

/**
     * 根据开始时间和结束时间返回时间段内的时间集合
     *
     * @param start
     * @param end
     * @return List
     */
    public static List<Date> getDatesBetweenTwoDate(String start, String end) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date beginDate = sdf.parse(start);
        Date endDate = sdf.parse(end);
        if (beginDate.compareTo(endDate) > 0) {
            Date tmp = endDate;
            endDate = beginDate;
            beginDate = tmp;
        }
        List<Date> lDate = new ArrayList<Date>();
        lDate.add(beginDate);// 把开始时间加入集合
        Calendar cal = Calendar.getInstance();
        // 使用给定的 Date 设置此 Calendar 的时间
        cal.setTime(beginDate);
        boolean bContinue = true;
        while (bContinue) {
            // 根据日历的规则,为给定的日历字段添加或减去指定的时间量
            cal.add(Calendar.DAY_OF_MONTH, 1);
            // 测试此日期是否在指定日期之后
            if (endDate.after(cal.getTime())) {
                lDate.add(cal.getTime());
            } else {
                break;
            }
        }
        lDate.add(endDate);// 把结束时间加入集合
        return lDate;
    }

计算中间月份代码:

 /**
     * 根据开始时间和结束时间返回时间段内的月份集合
     *
     * @param start
     * @param end
     * @return List
     */
    public static List<String> getMonthBetweenTwoDate(String start, String end) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date beginDate = sdf.parse(start);
        Date endDate = sdf.parse(end);
        if (beginDate.compareTo(endDate) > 0) {
            Date tmp = endDate;
            endDate = beginDate;
            beginDate = tmp;
        }
        List<String> lDate = new ArrayList<String>();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(beginDate);
        int startYear = calendar.get(Calendar.YEAR);
        int startMonth = calendar.get(Calendar.MONTH);
        calendar.setTime(endDate);
        int endYear = calendar.get(Calendar.YEAR);
        int endMonth = calendar.get(Calendar.MONTH);
        for (int i = startYear; i <= endYear; i++) {
            String date = "";
            if (startYear == endYear) {
                for (int j = startMonth; j <= endMonth; j++) {
                    if (j < 9) {
                        date = i+"-0"+ (j+1);
                    } else {
                        date = i+"-"+ (j+1);
                    }
                    lDate.add(date);
                }
            } else {
                if (i == startYear){
                    for (int j = startMonth; j < 12; j++) {
                        if (j < 9) {
                            date = i+"-0"+ (j+1);
                        } else {
                            date = i+"-"+ (j+1);
                        }
                        lDate.add(date);
                    }
                } else if (i == endYear) {
                    for (int j = 0; j <= endMonth; j++) {
                        if (j < 9) {
                            date = i+"-0"+ (j+1);
                        } else {
                            date = i+"-"+ (j+1);
                        }
                        lDate.add(date);
                    }
                } else {
                    for (int j = 0; j < 12; j++) {
                        if (j < 9) {
                            date = i+"-0"+ (j+1);
                        } else {
                            date = i+"-"+ (j+1);
                        }
                        lDate.add(date);
                    }
                }

            }
        }

        return lDate;
    }
public static void main(String[] args) throws Exception {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String start = "2018-01-03";
        String end = "2018-04-16";
        List<Date> listDate = getDatesBetweenTwoDate(start, end);
        for (int i = 0; i < listDate.size(); i++) {
            System.out.println(sdf.format(listDate.get(i)));
        }

        List<String> listMonth = getMonthBetweenTwoDate(start, end);
        for (String s : listMonth) {
            System.out.println(s);
        }
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值