カレンダー印刷ウィジェット

1500 ワード

ウィジェットコード:
import java.util.Calendar;
import java.util.GregorianCalendar;

public class StudyTest1 {
	public static void main(String[] args) {
		Calendar aCalender = new GregorianCalendar();
		int month = aCalender.get(Calendar.MONTH);
		int today = aCalender.get(Calendar.DAY_OF_MONTH);
		
		aCalender.set(Calendar.DAY_OF_MONTH, 1);
		int weekDay = aCalender.get(Calendar.DAY_OF_WEEK);
		
		System.out.println("SUN MON TUE WED THU FRI SAT");
		
		for(int i=Calendar.SUNDAY; i<weekDay; i++) {
			System.out.println("    ");
		}
		
		do {
			int day = aCalender.get(Calendar.DAY_OF_MONTH);
			
			System.out.printf("%3d", day);
			
			if(day == today) {
				System.out.print("*");
			} else {
				System.out.print(" ");
			}
			
			if(weekDay == Calendar.SATURDAY) {
				System.out.println();
			}
			
			aCalender.add(Calendar.DAY_OF_MONTH, 1);
			weekDay = aCalender.get(Calendar.DAY_OF_WEEK);
			
		} while (aCalender.get(Calendar.MONTH) == month);
		
		if(weekDay != Calendar.SUNDAY) {
			System.out.println();
		}
	}
}

印刷効果:
SUN MON TUE WED THU FRI SAT
  1   2*  3   4   5   6   7
  8   9  10  11  12  13  14
15  16  17  18  19  20  21
22  23  24  25  26  27  28
29  30  31