JAVA獲取某段時間內的所有日期

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author njchenyi
 */
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class FindDates {

    public static void main(String[] args) throws Exception {
        Calendar cal = Calendar.getInstance();
        String start = "2014-02-01";
        String end = "2014-03-02";
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date dBegin = sdf.parse(start);
        Date dEnd = sdf.parse(end);
        List<Date> lDate = findDates(dBegin, dEnd);
        for (Date date : lDate) {
            System.out.println(sdf.format(date));
        }
    }

    public static List<Date> findDates(Date dBegin, Date dEnd) {
        List lDate = new ArrayList();
        //lDate.add(dBegin);
        Calendar calBegin = Calendar.getInstance();
        // 使用給定的 Date 設置此 Calendar 的時間  
        calBegin.setTime(dBegin);
        Calendar calEnd = Calendar.getInstance();
        // 使用給定的 Date 設置此 Calendar 的時間  
        calEnd.setTime(dEnd);
        // 測試此日期是否在指定日期之後  
        while (dEnd.after(calBegin.getTime())) {
<pre code_snippet_id="455935" snippet_file_name="blog_20140821_1_748267" name="code" class="java">            lDate.add(calBegin.getTime());
// 根據日曆的規則,爲給定的日曆字段添加或減去指定的時間量 calBegin.add(Calendar.DAY_OF_MONTH, 1); } return lDate; }}



            lDate.add(calBegin.getTime());
發佈了298 篇原創文章 · 獲贊 78 · 訪問量 289萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章