这个打印日历的程序我觉得特别难理解。。
import java.text.DateFormatSymbols;import java.util.*;
class CalendarTest
{
public static void main(String[] args)
{
//Locale.setDefault(Locale.CHINA);
GregorianCalendar d = new GregorianCalendar();//创建当前日期
int today = d.get(Calendar.DAY_OF_MONTH); //当前日
int month = d.get(Calendar.MONTH);
//System.out.println(today + " " + month);
d.set(Calendar.DAY_OF_MONTH,1); //将d设置为月初
int weekday = d.get(Calendar.DAY_OF_WEEK);//该周的第几天
//System.out.println(weekday);//月初是第二个元素 周一
int firstDayOfWeek = d.getFirstDayOfWeek();//获得本地的周的起始 第一个元素 周日
//System.out.println(firstDayOfWeek);
int indent = 0;//缩排
while(weekday != firstDayOfWeek)//2 != 1
{
indent++;
d.add(Calendar.DAY_OF_MONTH,-1);
weekday = d.get(Calendar.DAY_OF_WEEK);//1
}
//d目前在周日的位置
String[] weekdayNames = new DateFormatSymbols().getShortWeekdays();
//System.out.println(weekday);
//打印表头
do
{
System.out.printf("%4s",weekdayNames[weekday]);
d.add(Calendar.DAY_OF_MONTH,1);
weekday = d.get(Calendar.DAY_OF_WEEK);
}
while (weekday != firstDayOfWeek);//
System.out.println();
//打印月初之前的空格
for (int i = 1 ; i <= indent ; i++ )
{
System.out.printf(" ");
}
d.set(Calendar.DAY_OF_MONTH,1);//?
do
{
//打印日
int day = d.get(Calendar.DAY_OF_MONTH);
System.out.printf("%6d",day);
//标记当前日
if(day == today)
{
System.out.printf("*");
}
else
{
System.out.printf(" ");
}
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);
}
}