ある年のすべての火曜日の日付を取得

754 ワード

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;


public class Test {
	public static void main(String[] args) {
		int year = 2010;
		Calendar calendar = Calendar.getInstance();
		DateFormat df = new SimpleDateFormat("yyyyMMdd");
		calendar.set(year, 0, 1);
		while (true) {
			if (calendar.get(Calendar.YEAR) != year) {
				break;
			}
			if (calendar.get(Calendar.DAY_OF_WEEK) == 3) { // 1=    2=    3=    4=    5=    6=    7=   
				System.out.println(df.format(calendar.getTime()));
			}
			calendar.set(Calendar.DATE, calendar.get(Calendar.DATE)+1);
		}
	}
}