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

来源:互联网 发布:软件系统应急预案 编辑:程序博客网 时间:2024/04/28 20:19
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 blocke.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;    }} 

原创粉丝点击