java-两个日期之间的所有日期

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @author 作者 E-mail: ZH519080@163.com
 * @date 创建时间:2016年11月4日 上午10:49:37
 * @jdk 版本:jdk1.7.0_79
 * @类说明:获取某个日期与当前日期之间的所有日期
 */
public class GetBetweenDate {

	public static List<Date> getDatesBetweenTwoDate(Date beginDate, Date endDate) {
		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;
	}

	public String executeDate() {

		SimpleDateFormat sDateFormat = new SimpleDateFormat("yyyy-MM-dd");

		String start = "2017-01-01";
		
		String end = "2017-07-12";

		String dateList = "";

		try {
			Date beginDate = sDateFormat.parse(start);
			Date endDate = sDateFormat.parse(end);

			List<Date> datesBetweenTwoDate = getDatesBetweenTwoDate(beginDate, endDate);
			for(int i = 0;i < datesBetweenTwoDate.size();i++){
		    	/*
		    	 * 在此要特别注意:
		    	 * integralURL += invest+sDateFormat.format(datesBetweenTwoDate.get(i))+","得到的结果是全部的字符串拼接成
		    	 * 的一个新字符串,
		    	 * 而
		    	 * integralURL = invest+sDateFormat.format(datesBetweenTwoDate.get(i))+","得到的结果只是
		    	 * 当i为最大值的字符串,
		    	 */
				dateList += sDateFormat.format(datesBetweenTwoDate.get(i))+",";
			}
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return dateList.substring(0, dateList.length()-1);

	}

//	public static void main(String[] args) throws ParseException {
//		GetBetweenDate getBetweenDate = new GetBetweenDate();
//		String execute = getBetweenDate.execute();
//		System.out.println(execute.substring(0, execute.length() - 1));
//	}

}






import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

/** 
* @author 作者 E-mail: ZH519080@163.com
* @date 创建时间:2016年12月29日 下午1:48:06 
* @jdk 版本:jdk1.7.0_79
*
* @类说明:
*/
public class GetBetweenMonth {
    
    //执行两个月份之间的所有月份
    public String executeMonth(){
        
        String startMonth = "2016-01";
        String endMonth = "2016-11";
        String monthList = "";
        List<String> monthBetween = getMonthBetween(startMonth, endMonth);
        for(int i = 0;i < monthBetween.size();i++){
            monthList += monthBetween.get(i)+",";
        }
        return monthList.substring(0, monthList.length()-1);
    }
    
    //获取两个月份之间的所有月份
    private static List<String> getMonthBetween(String startMonth, String endMonth) {
        ArrayList<String> result = new ArrayList<String>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");// 格式化为年月

        try {
            Calendar min = Calendar.getInstance();
            Calendar max = Calendar.getInstance();

            min.setTime(sdf.parse(startMonth));
            min.set(min.get(Calendar.YEAR), min.get(Calendar.MONTH), 1);

            max.setTime(sdf.parse(endMonth));
            max.set(max.get(Calendar.YEAR), max.get(Calendar.MONTH), 2);

            Calendar curr = min;
            while (curr.before(max)) {
                result.add(sdf.format(curr.getTime()));
                curr.add(Calendar.MONTH, 1);
            }
        } catch (Exception e) {
            // TODO: handle exception
        }

        return result;
    }

}




 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值