import javatextDateFormatSymbols; import javautilCalendar; import javautilGregorianCalendar; import javautilLocale;
public class CalenderTest {
/** * 本程序是打印一个当前月历的示例 * * @Version // * @author Eduardo */ public static void main(String[] args) { //将本地区设置为美国地区为了输出英文星期格式 LocalesetDefault(LocaleUS); //构造一个日历对象 GregorianCalendar d = new GregorianCalendar();
int today =dget(CalendarDAY_OF_MONTH);//today为获取当前的日 int month=dget(CalendarMONTH);//month为获取当前的月 //将d设置为本月号 dset(CalendarDAY_OF_MONTH);
int weekday=dget(CalendarDAY_OF_WEEK);//weekday为获取本月号为星期几 //firstDayOfWeek为获取本地区的起始日 int firstDayOfWeek=dgetFirstDayOfWeek();
/*进行缩进将日历对象的日减一知道一个星期的第一天位置*/ int indent=;//记录缩进几次 while(weekday!=firstDayOfWeek) { indent++; dadd(CalendarDAY_OF_MONTH );//当前日期提前一天 weekday=dget(CalendarDAY_OF_WEEK);//获取当前日期为星期几 }
/*打印星期缩写字符串*/ String[] weekdayNames=new DateFormatSymbols()getShortWeekdays();//输出表示星期的方式为缩写 do { Systemoutprintf(%sweekdayNames[weekday]); dadd(CalendarDAY_OF_MONTH);//日期增移动 weekday=dget(CalendarDAY_OF_WEEK);//获取当前日期为星期几 } while(weekday!=firstDayOfWeek); Systemoutprintln();
/*本月号前的日期补位空格*/ for(int i=;i<=indent;i++) Systemoutprint( );
/*进行打印日期当日日期后用*注释直到d进入下一个月停止打印*/ dset(CalendarDAY_OF_MONTH );//将d设置为本月号 do { int day=dget(CalendarDAY_OF_MONTH); Systemoutprintf(%d day);
if(day==today)Systemoutprint(*); else Systemoutprint( );
dadd(CalendarDAY_OF_MONTH ); weekday=dget(CalendarDAY_OF_WEEK); if(weekday==firstDayOfWeek)Systemoutprintln();//weekday为星期的第一天则换行打印 } while(dget(CalendarMONTH)==month);
if(weekday!=firstDayOfWeek)Systemoutprintln();
}
} |