/**
* 程序运行当年的日历,程序运行当日以*号表示
*/
import java.util.*;
public class CalendarTest {
public static void main(String[] args) {
GregorianCalendar d = new GregorianCalendar();
int year = d.get(Calendar.YEAR);
int month = d.get(Calendar.MONTH);
int today = d.get(Calendar.DAY_OF_YEAR);
d.set(d.get(Calendar.YEAR),0,1); //设置为当年1月1日(0表示1月)
do {
System.out.println(d.get(Calendar.MONTH) + 1 + "月");
System.out.println("Sun Mon Tue Wed Thu Fri Sat"); //输出月表头
month = d.get(Calendar.MONTH); //取得月份
int weekday = d.get(Calendar.DAY_OF_WEEK); //获得当月1号,在一周中是星期几
for(int i = Calendar.SUNDAY; i < weekday; i++)//控制当月1号的起始位置
System.out.print(" ");
do {
weekday = d.get(Calendar.DAY_OF_WEEK);
if (d.get(Calendar.DAY_OF_MONTH) < 10) //如果日期小于10,多输出一个空格,以便对齐
System.out.print(" ");
System.out.print(d.get(Calendar.DAY_OF_MONTH));//输出日期
if (today == d.get(Calendar.DAY_OF_YEAR)) //如果是当日,则输出一个*号
System.out.print("* ");
else
System.out.print(" ");
if (weekday == Calendar.SATURDAY) //到达周六,则换行
System.out.println();
d.add(Calendar.DAY_OF_YEAR,1); //日期累加
} while (d.get(Calendar.MONTH) == month); //如果还是当月,继续循环
System.out.println("\n");
} while (d.get(Calendar.YEAR) == year); //如果还是当年,继续循环
}
}
posted on 2006-07-22 17:17
wqwqwqwqwq 阅读(353)
评论(0) 编辑 收藏 所属分类:
Simple Java