簡單的日曆小程式

來源:互聯網
上載者:User

標籤:

需求:在控制台輸入年月(yyyy-MM),則列印出該年該月的行事曆資訊:

分析:

1:計算1900年1月1日距離輸入日期的天數,日數可以算作1日

2:總天數對7取餘應該是該天所在周數

3:然後根據顯示該月的列印資訊

具體代碼如下:

主方法類:

 1 /** 2  *  3  */ 4 package com.hlcui.cal; 5  6 import java.util.Scanner; 7  8 import com.hlcui.util.CalendarUtil; 9 10 /**11  * @author Administrator 類比製作一個日曆系統 1:計算1900年1月1日到現在一個多少天 2:用天數除以7是否等於今天星期幾12  */13 public class CalendarSys {14 15     /**16      * 處理要求方法17      * 18      * @param year19      * @param month20      */21     public static void handler(int year, int month) {22 23         int sumDays = CalendarUtil.getDaysOn1900(year, month);24 25         // 計算日累計的天數26         sumDays += 1; // 從1號開始計算天數27 28         // 計算當月的天數29         int days = CalendarUtil.getDays(year, month);30 31         // 列印日曆表32         System.out.println("日\t一\t二\t三\t四\t五\t六\t");33 34         for (int w = 0; w < sumDays % 7; w++) {35             System.out.print("\t");36         }37 38         for (int m = 1; m <= days; m++, sumDays++) {39             if (sumDays % 7 == 6) {40                 System.out.println(m);41             } else {42                 System.out.print(m + "\t");43             }44         }45 46     }47 48     public static void main(String[] args) {49 50         /**51          * 使用者互動52          */53         @SuppressWarnings("resource")54         Scanner scan = new Scanner(System.in);55         System.out.println("請輸入日期:<yyyy-MM>");56         String date = scan.next();57         int[] d = CalendarUtil.getYearAndMonth(date);58         int year = d[0];59         int month = d[1];60         // System.out.println("請輸入今天是幾號:"); 驗證計算的天數是否正確61         // int day = scan.nextInt();62         handler(year, month);63 64     }65 }

工具類代碼:

 1 /** 2  *  3  */ 4 package com.hlcui.util; 5  6 /** 7  * @author Administrator 8  *  9  */10 public class CalendarUtil {11 12     /**13      * 判斷是否閏年的方法14      */15     public static boolean isYear(int year) {16         if (year <= 0) {17             System.out.println("請輸入正確的年份:");18         }19         if (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0)) {20             return true;21         }22 23         return false;24 25     }26 27     /**28      * 根據年份和月份計算天數29      */30     public static int getDays(int year, int month) {31 32         int days = 0;33 34         if (month == 2) {35             if (CalendarUtil.isYear(year)) {36                 days = 29;37             } else {38                 days = 28;39             }40         } else if (month == 4 || month == 6 || month == 9 || month == 11) {41             days = 30;42         } else {43             days = 31;44         }45         return days;46 47     }48 49     /**50      * 計算從1900年1月 到 x年x月的天數51      */52     public static int getDaysOn1900(int endYear, int endMonth) {53 54         int sumDays = 0;55 56         // 計算年累計的天數57         for (int y = 1900; y < endYear; y++) {58             if (CalendarUtil.isYear(y)) {59                 sumDays += 366;60             } else {61                 sumDays += 365;62             }63         }64 65         // 計算月累計的天數66         for (int m = 1; m < endMonth; m++) {67             if (m == 2) {68                 if (CalendarUtil.isYear(endYear)) {69                     sumDays += 29;70                 } else {71                     sumDays += 28;72                 }73             } else if (m == 4 || m == 6 || m == 9 || m == 11) {74                 sumDays += 30;75             } else {76                 sumDays += 31;77             }78         }79         return sumDays;80     }81 82     /**83      * 根據日期擷取對應年份和月份84      */85     public static int[] getYearAndMonth(String date) {86         String[] str = date.split("-");87         int[] irr = new int[str.length];88         for (int x = 0; x < str.length; x++) {89             irr[x] = Integer.parseInt(str[x]);90         }91         return irr;92     }93 }

效果顯示:

和日曆表進行比較,是一致的。如下:

簡單的日曆小程式

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.