簡單的日歷小程序


需求:在控制台輸入年月(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 year
19      * @param month
20      */
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 }

效果顯示:

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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM