package com.tcs.example;
import java.text.DateFormatSymbols;
import java.util.Calendar;
import java.util.GregorianCalendar;
public class CalendarTest {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
GregorianCalendar d = new GregorianCalendar();
int today = d.get(Calendar.DAY_OF_MONTH);
int month = d.get(Calendar.MONTH)+1;
d.set(Calendar.DAY_OF_MONTH, 1);
d.add(Calendar.MONTH, 1);
int weekday = d.get(Calendar.DAY_OF_WEEK);
int firstDayOfWeek = d.getFirstDayOfWeek();
int indent = 0;
while (weekday != firstDayOfWeek) {
indent++;
d.add(Calendar.DAY_OF_MONTH, -1);
weekday = d.get(Calendar.DAY_OF_WEEK);
}
// while(weekday != firstDayOfWeek);
String[] weekdayName = new DateFormatSymbols().getShortWeekdays();
for (int i = 1; i < weekdayName.length; i++) {
System.out.print(weekdayName[i] +" ");
}
System.out.println();
for (int i = 1; i <= indent; i++) {
System.out.print(" ");
}
d.set(Calendar.DAY_OF_MONTH, 1);
do {
int day = d.get(Calendar.DAY_OF_MONTH);
System.out.printf("%3d", day);
if (day == today) {
System.out.print("* ");
} else {
System.out.print(" ");
}
d.add(Calendar.DAY_OF_MONTH, 1);
weekday = d.get(Calendar.DAY_OF_WEEK);
if (weekday == firstDayOfWeek) {
System.out.println();
}
}
while (d.get(Calendar.MONTH) != month);
}
}
结果:
日 一 二 三 四 五 六
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