首页 > 代码库 > Java获取两个时间段内的所有日期

Java获取两个时间段内的所有日期

import java.text.SimpleDateFormat;import java.util.ArrayList;import java.util.Calendar;import java.util.Date;import java.util.List;/** * 获取两个时间段内的所有日期,日期可跨年 */public class GetBetweenDate {    public static void main(String[] args) {        List<String> list = getBetweenDate("2013-12-01","2014-02-01");        for(String date:list){            System.out.println(date);        }    }    public static List<String> getBetweenDate(String begin,String end){        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");        List<String> betweenList = new ArrayList<String>();                try{            Calendar startDay = Calendar.getInstance();            startDay.setTime(format.parse(begin));            startDay.add(Calendar.DATE, -1);                        while(true){                startDay.add(Calendar.DATE, 1);                Date newDate = startDay.getTime();                String newend=format.format(newDate);                betweenList.add(newend);                if(end.equals(newend)){                    break;                }            }        }catch (Exception e) {            e.printStackTrace();        }                return betweenList;    }}

 

Java获取两个时间段内的所有日期