Java计算某月第几个星期几的日期

Java计算某月第几个星期几的日期

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

public class TheDate {
    /*
     * 计算某年某月第几个星期几的日期 
     * param wek 星期几 
     * param num 第几个,10为最后一个
     */
    public static String getTheDate(int year, int month, int wek, int num) {
        Calendar time = Calendar.getInstance();
        time.set(Calendar.YEAR, year);
        time.set(Calendar.MONTH, month - 1);// 注意,Calendar对象默认一月为0
        int day = time.getActualMaximum(Calendar.DAY_OF_MONTH);// 本月份的天数
        List<String> list = new ArrayList<String>();
        String dateTime = "";
        for (int i = 1; i <= day; i++) {
            time.set(Calendar.DAY_OF_MONTH, i);
            int weekd = time.get(Calendar.DAY_OF_WEEK) - 1;// 注意,Calendar对象默认星期天为1
            if (wek >= 1 && wek <= 7) {
                if (wek < 7) {
                    if (weekd == wek) {
                        String aaa = year + "-" + month + "-" + i;
                        list.add(aaa);
                    }
                } else {
                    if (weekd == 0) {
                        String aaa = year + "-" + month + "-" + i;
                        list.add(aaa);
                    }
                }

            }

        }
        if (list.size() > 0 && num <= 5) {
            dateTime = list.get(num - 1);
        } else if (num == 10) {
            dateTime = list.get(list.size() - 1);
        }
        return dateTime;
    }

    public static void main(String[] args) {
        //比如父亲节:6月的第三个星期日
        System.out.println(getTheDate(2015, 6, 7, 3));
    }

输出:2015-6-21

posted @ 2015-10-10 15:20  harry_ma  阅读(1177)  评论(0编辑  收藏  举报
 aa
    部分内容来自网络,如有异议,请联系我.     


      成绩来自坚持, 灵感来自激情。

                                                               感谢父母和亲朋好友的爱与支持!