• 简单的日历小程序


    需求:在控制台输入年月(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("日	一	二	三	四	五	六	");
    33 
    34         for (int w = 0; w < sumDays % 7; w++) {
    35             System.out.print("	");
    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 + "	");
    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 }

    效果显示:

    和日历表进行比较,是一致的。如下:

  • 相关阅读:
    golang 类型断言的学习
    如何查询每个用户的第二条记录
    PHP Slim 框架初体验之无法访问控制器
    jquery循环遍历radio单选按钮,并设置选中状态
    CI框架中自定义view文件夹位置
    PHP代码实现MySQL读写分离
    mysql实现主从复制
    wildflyのデプロイ後の保存位置
    postgresql function
    shell backup
  • 原文地址:https://www.cnblogs.com/warrior4236/p/5460214.html
Copyright © 2020-2023  润新知