算法:日历

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

public class $ {

    private static int[] DAYS = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };

    public static void main(String... _) {

        long l1 = System.currentTimeMillis();
        List data = rili(1000, 1, 10000, 12);
        long l2 = System.currentTimeMillis();

        for (int i = 0; i < data.size(); i++) {
            System.out.println(data.get(i));
        }
        System.out.println((l2 - l1) + "MS");
    }

    private static List rili(int startYear, int startMonth, int endYear, int endMonth) {

        if (startYear > endYear || (startYear == endYear && startMonth > endMonth)) {
            return null;
        }
        List data = new ArrayList();

        for (int ii = startYear; ii <= endYear; ii++) {

            int startM = startMonth - 1;
            int endM = endMonth - 1;

            if (startYear < endYear) {
                if (ii == endYear) {
                    endM = 11;
                } else {
                    startM = 0;
                }
            }
            for (int i = startM; i <= endM; i++) {
                data.add("-----------------------" + ii + "年" + (i + 1) + "月-----------------------");
                data.add("日\t一\t二\t三\t四\t五\t六");

                int day = days(ii, i);
                StringBuffer buf = new StringBuffer();
                int idx = 1;
                while (idx <= day) {
                    int len = 0;
                    if (idx == 1) {
                        Calendar c = Calendar.getInstance();
                        c.set(ii, i, idx);
                        int xingqi = c.get(Calendar.DAY_OF_WEEK);
                        len = getLen(xingqi);
                        buf.append(addBlank(len));
                    }
                    buf.append(idx++);
                    for (int k = len + 1; k < 7; k++) {
                        if (idx > day) {
                            break;
                        }
                        buf.append("\t" + (idx++));
                    }
                    buf.append("\n");
                }
                data.add(buf);
            }
        }
        return data;
    }

    private static StringBuffer addBlank(int len) {
        StringBuffer buf = new StringBuffer();
        for (int index = 0; index < len; index++) {
            buf.append("\t");
        }
        return buf;
    }

    private static int getLen(int xingqi) {
        return xingqi == Calendar.SUNDAY ? 0 : xingqi - 1;
    }

    public static int days(int year, int month) {

        if (month != 1) {
            return DAYS[month];
        }

        if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
            return 29;
        }
        return DAYS[month];
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值