- /**
- *@authorbzwm
- *
- */
- importjava.io.BufferedReader;
- importjava.io.IOException;
- importjava.io.InputStreamReader;
- importjava.util.Calendar;
- importjava.util.GregorianCalendar;
- publicclassCalendarTest{
- publicstaticvoidmain(String[]args)throwsIOException{
- BufferedReaderin=newBufferedReader(newInputStreamReader(System.in));
- System.out.print("请输入一个年份:");
- Stringyears=in.readLine();
- CalendarPrintercp=newCalendarPrinter(years);
- cp.printCal();
- }
- }
- classCalendarPrinter{
- privateintyear;
- privatestaticfinalintmonthCount=12;
- publicCalendarPrinter(Stringyears){
- if(!years.matches("//d{4}")){
- System.out.println("yearthatinputtedisillagel.");
- return;
- }
- year=Integer.parseInt(years);
- }
- publicvoidprintCal(){
- //constructdascurrentdate
- GregorianCalendargCal=newGregorianCalendar();
- //setyear
- gCal.set(Calendar.YEAR,year);
- for(intmonth=0;month<monthCount;month++){
- gCal.set(Calendar.MONTH,month);
- printOut(gCal);
- }
- }
- privatevoidprintOut(Calendarcal){
- intmonth=cal.get(Calendar.MONTH);
- //setcaltostartdateofthemonth
- cal.set(Calendar.DAY_OF_MONTH,1);
- intweekday=cal.get(Calendar.DAY_OF_WEEK);
- //printheading
- System.out.println("SunMonTueWedThuFriSat");
- //indentfirstlineofcalendar
- for(inti=Calendar.SUNDAY;i<weekday;i++)
- System.out.print("");
- do{
- //printday
- intday=cal.get(Calendar.DAY_OF_MONTH);
- if(day>0)
- System.out.print(""+day+"");
- else
- System.out.print(""+day+"");
- //startanewlineaftereverySaturday
- if(weekday==Calendar.SATURDAY)
- System.out.println();
- //advancedtothenextday
- cal.add(Calendar.DAY_OF_MONTH,1);
- weekday=cal.get(Calendar.DAY_OF_WEEK);
- }while(cal.get(Calendar.MONTH)==month);
- //theloopexitswhendisday1ofthenextmonth
- //printfinalendoflineifnecessary
- if(weekday!=Calendar.SUNDAY)
- System.out.println();
- }
- }
利用Calendar输出指定年份的全年日历
最新推荐文章于 2021-06-29 16:09:15 发布