ALL is Well!

          敏捷是一條很長的路,摸索著前進著

            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理 ::
            30 隨筆 :: 23 文章 :: 71 評論 :: 0 Trackbacks
          /**
           * 
          @author bzwm
           * 
           
          */

          import java.io.BufferedReader;
          import java.io.IOException;
          import java.io.InputStreamReader;
          import java.util.Calendar;
          import java.util.GregorianCalendar;

          public class CalendarTest {
              
          public static void main(String[] args) throws IOException {
                  BufferedReader in 
          = new BufferedReader(new InputStreamReader(System.in));
                  System.out.print(
          "請輸入一個年份:");
                  String years 
          = in.readLine();
                  CalendarPrinter cp 
          = new CalendarPrinter(years);
                  cp.printCal();
              }

          }


          class CalendarPrinter {

              
          private int year;

              
          private static final int monthCount = 12;

              
          public CalendarPrinter(String years) {
                  
          if (!years.matches("\\d{4}")) {
                      System.out.println(
          "year that inputted is illagel.");
                      
          return;
                  }

                  year 
          = Integer.parseInt(years);
              }


              
          public void printCal() {
                  
          // construct d as current date
                  GregorianCalendar gCal = new GregorianCalendar();

                  
          //set year
                  gCal.set(Calendar.YEAR, year);
                  
          for (int month = 0; month < monthCount; month++{
                      gCal.set(Calendar.MONTH, month);
                      printOut(gCal);
                  }

              }


              
          private void printOut(Calendar cal) {
                  
          int month = cal.get(Calendar.MONTH);

                  
          // set cal to start date of the month
                  cal.set(Calendar.DAY_OF_MONTH, 1);

                  
          int weekday = cal.get(Calendar.DAY_OF_WEEK);

                  
          // print heading
                  System.out.println("Sun Mon Tue Wed Thu Fri Sat");

                  
          // indent first line of calendar
                  for (int i = Calendar.SUNDAY; i < weekday; i++)
                      System.out.print(
          "    ");

                  
          do {
                      
          // print day
                      int day = cal.get(Calendar.DAY_OF_MONTH);
                      
          if (day > 0)
                          System.out.print(
          " " + day + " ");
                      
          else
                          System.out.print(
          "  " + day + " ");

                      
          // start a new line after every Saturday
                      if (weekday == Calendar.SATURDAY)
                          System.out.println();

                      
          // advance d to the next day
                      cal.add(Calendar.DAY_OF_MONTH, 1);
                      weekday 
          = cal.get(Calendar.DAY_OF_WEEK);
                  }
           while (cal.get(Calendar.MONTH) == month);
                  
          // the loop exits when d is day 1 of the next month

                  
          // print final end of line if necessary
                  if (weekday != Calendar.SUNDAY)
                      System.out.println();
              }

          }

          ----2008年12月01日
          posted on 2010-09-01 11:09 李 明 閱讀(412) 評論(0)  編輯  收藏 所屬分類: Java
          主站蜘蛛池模板: 民权县| 西青区| 皋兰县| 和硕县| 诸暨市| 阿克陶县| 蛟河市| 临武县| 望江县| 边坝县| 前郭尔| 屏东县| 邢台市| 二连浩特市| 通江县| 象山县| 西平县| 吉林省| 邢台市| 永昌县| 福安市| 涿州市| 革吉县| 兰考县| 光山县| 西丰县| 尼玛县| 信阳市| 凤庆县| 武义县| 南宁市| 鹿泉市| 东莞市| 云林县| 伊金霍洛旗| 万源市| 格尔木市| 岳池县| 施秉县| 乌审旗| 阿拉善左旗|