Java遍历日期代码

import java.util.ArrayList;
import java.util.List;

public class DateTraveller {
    
    public static List<String> getDateList(String startDateString, String endDateString) {
        List<String> dateList = new ArrayList<String>();
        // start date
        int startDate = Integer.parseInt(startDateString);
        int startYear = startDate / 10000;
        int startMonth = startDate / 100 % 100;
        int startDay = startDate % 100;
        // end date
        int endDate = Integer.parseInt(endDateString);
        int endYear = endDate / 10000;
        int endMonth = endDate / 100 % 100;
        int endDay = endDate % 100;
        // begin
        int y = startYear;
        int m = startMonth;
        int d = startDay;
        while (y < endYear || y == endYear && m < endMonth || y == endYear && m == endMonth && d <= endDay) {
//          System.out.println(y + "-" + m + "-" + d + " vs. " + endYear + "-" + endMonth + "-" + endDay + " : " + 
//                  (y < endYear) + "," +  (y == endYear && m < endMonth) + "," + (y == endYear && m == endMonth && d <= endDay)
//                  );
            String tmpDateString = String.format("%d%02d%02d", y, m, d);
            dateList.add(tmpDateString);
            boolean isRunNian = (y % 400 == 0 || y % 4 == 0 && y % 100 != 0);
            int lastDay = 31;
            if (m == 2) {
                if (isRunNian) lastDay = 29;
                else lastDay = 28;
            } 
            else if (m <= 7 && m % 2 == 0 || m > 7 && m % 2 == 1) {
                lastDay = 30;
            }
            if (d >= lastDay) {
                m ++;
                d = 1;
                if (m > 12) {
                    y ++;
                    m = 1;
                }
            }
            else {
                d ++;
            }
        }
        return dateList;
    }
    
    // test
    public static void main(String[] args) {
        List<String> dateList = getDateList("20151203", "20160301");
        for (String dateS : dateList) {
            System.out.println(dateS);
        }
    }
    
}

转载于:https://www.cnblogs.com/zifeiy/p/9697649.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值