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