Java统计两个日期时间段每个月对应的天数

现在有这样一个需求:

统计两个日期时间段每个月对应的天数
比如:2017-03-12 ~ 2018-12-18
2017-03-12到该月月底共有多少天
2017-04-01到该月月底有多少天

。。。


代码实现:

/**
 * Created by hjz on 2017/8/7 0007.
 * Describe:
 * 统计两个日期时间段每个月对应的天数
 * 比如:2017-03-12 ~ 2018-12-18
 * 2017-03-12到该月月底共有多少天
 * 2017-04该月有多少天
 * 。。。
 */
public class TimeUtils {

    public static void main(String[] args) throws Exception {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

//        Date maxDate = sdf.parse("2017-12-23");
        Date maxDate = sdf.parse("2015-12-12");
        Date minDate = sdf.parse("2015-12-12");


        Calendar max = Calendar.getInstance();
        max.setTime(maxDate);
        max.set(max.get(Calendar.YEAR), max.get(Calendar.MONTH), 2);

        Calendar min = Calendar.getInstance();
        min.setTime(minDate);
        min.set(min.get(Calendar.YEAR), min.get(Calendar.MONTH), 1);

        Calendar curr = min;
        List<Date> result = new ArrayList<>();
        while (curr.before(max)) {
            result.add(curr.getTime());
            curr.add(Calendar.MONTH, 1);
        }

        String day = "";
        Date minMothDate = null;
        Date maxMothDate = null;
        Calendar currMonthCal = Calendar.getInstance();
        for (int i=0; i<result.size(); i++){
            if (i == 0){  //处理开始日期时间
                if (result.size() == 1){   //处理开始时间和结束时间是同年同月的情况
                    minMothDate = minDate;
                    maxMothDate = maxDate;
                }else {
                    minMothDate = minDate;
                    currMonthCal.setTime(result.get(i));
                    //设置当前月最后一天
                    currMonthCal.set(Calendar.DAY_OF_MONTH, currMonthCal.getActualMaximum(Calendar.DAY_OF_MONTH));
                    maxMothDate = currMonthCal.getTime();
                }
                day = getTwoDay(maxMothDate,minMothDate);
            }else if (i == result.size()-1){ //处理最后一次时间
                maxMothDate = maxDate;
                currMonthCal.setTime(result.get(i));
                currMonthCal.set(Calendar.DAY_OF_MONTH,1);//设置为1号,当前日期既为本月第一天
                minMothDate = currMonthCal.getTime();
                day = getTwoDay(maxMothDate,minMothDate);
            }else{
                currMonthCal.setTime(result.get(i));
                currMonthCal.set(Calendar.DAY_OF_MONTH,1);//设置为1号,当前日期既为本月第一天
                minMothDate = currMonthCal.getTime();

                //设置当前月最后一天
                currMonthCal.set(Calendar.DAY_OF_MONTH, currMonthCal.getActualMaximum(Calendar.DAY_OF_MONTH));
                maxMothDate = currMonthCal.getTime();

                day = getTwoDay(maxMothDate,minMothDate);
            }
            System.out.println(currMonthCal.get(Calendar.YEAR )+"年"+(currMonthCal.get(Calendar.MONTH )+1)+"月共 "+day +" 天");
        }


    }

    /**
     * 得到二个日期间的间隔天数
     * @param endTime  结束时间
     * @param startTime 开始时间
     * @return
     */
    public static String getTwoDay(Date endTime, Date startTime) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd", Locale.US);
        System.out.println( "startTime="+format.format(startTime)+ " endTime="+format.format(endTime) );
        long day = 0;
        try {
            day = (endTime.getTime() - startTime.getTime()) / (24 * 60 * 60 * 1000)+1;
        } catch (Exception e) {
            return "";
        }
        return day + "";
    }
}




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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值