用java製作日曆,想休息的時候看一看離周末還有幾天!

來源:互聯網
上載者:User

標籤:amp   new   pac   case   lda   java   sys   count   閏年   

     呀!忙碌的每一天,都忘記了明天就是我們愉快周末了。今天沒有朋友的閑聊的話,明天處在繃緊的狀態呢!還有朋友提到,所有今天來跟大家分享一下用java來編寫日曆表,累了想休息了的時候,開啟看看,還有幾天到周末!那麼我們來看一下代碼,具體怎麼實現吧!

public class Demo6 {

      public static void main(String[] args) {

               Demo6 demo = new Demo6();
               demo.show(2016,11);  //輸入年和月份
      }

     //定義一個方法:判斷是平年還是閏年
      public boolean isLeapYear(int year) {
                if (year%400==0 || (year%4==0 && year%100 !=0)) {
                       return true;
                 }

       return false;
       }

        //計算輸入年份,月份的總天數
        public int getTotalDateFrom(int year, int month) {
                 int totalDate = 0;
                 for (int i = 1900; i < year; i++) {

                           //判斷是閏年還是平年
                           if (isLeapYear(year)) {
                                    totalDate += 366;
                           }else{
                                    totalDate += 365;
                            }
                 }
                //計算2月份的天數
                for (int i = 1; i < month; i++) {
                           totalDate += getDayOfMonth(year, month);
                }
                return totalDate;

          }

          //返回一個天數
         public int getDayOfMonth(int year,int month) {
         switch (month) {
               case 1:
               case 3:
               case 5:
               case 7:
               case 8:
               case 10:
               case 12:
                           return 31;
               case 4:
               case 9:
               case 11:
                          return 30;
               case 2:
                        if (isLeapYear(year)) {
                                 return 29;
                        } else {
                                return 28;
                        }

               default:
                        return 30;
           }

         }

         public int printSpace(int year, int month){
                  return getTotalDateFrom(year, month)%7;
         }

         public void print(int year, int month){
                  int countSpac = printSpace(year, month)+1;   //空格的個數
                  int coun = 0;      //定義一個計數
                  for (int i = 0; i < countSpac; i++) {
                           coun ++;
                           System.out.print("\t");
                  }

                  //列印日期
                  for (int i = 0; i < getDayOfMonth(year, month); i++) {
                          if (coun % 7 == 0) {
                          System.out.println();
                          }
                          coun++;
                          System.out.print(i+"\t");
                  }

            }

           public void show(int year, int month){
                    System.out.println(" **************"+year+"******"+month+"***************");
                    System.out.println("=======================================================");
                    System.out.println("日\t一\t二\t三\t四\t五\t六");
                    System.out.println("=======================================================");
                    print(year, month);
                   System.out.println();
           }

}

 

實現效果如下:

 

用java製作日曆,想休息的時候看一看離周末還有幾天!

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.