• Java控制台打印简单日历


    打印任一正数年每一月的日历:                                       

    注意:公元元年1月1日是星期六

      1 import java.io.BufferedReader;
      2 import java.io.IOException;
      3 import java.io.InputStreamReader;
      4 
      5 public class TestCal {
      6     static int year, monthDay, weekDay;
      7 
      8     public static boolean isLeapYear(int y) {
      9         return ((y % 4 == 0 && y % 100 != 0) || (y % 400 == 0));// 判断闰年
     10     }
     11 
     12     /**
     13      * 判断该年第一天是星期几
     14      * 
     15      * @param y
     16      */
     17     public static int firstDay(int y) {
     18         long n = y * 365;
     19         for (int i = 1; i < y; i++) {
     20             if (isLeapYear(y)) {
     21                 n++;
     22             }
     23         }
     24 
     25         return (int) (n  - 2)%7;//公元1年1月1日是星期六
     26     }
     27 
     28     /**
     29      * 打印表头
     30      */
     31     public static void printWeek() {
     32         System.out.println("================================");
     33         System.out.println("日      一     二     三     四     五     六");
     34     }
     35 
     36     /**
     37      * 获取每个月的天数
     38      * @param m
     39      * @return
     40      */
     41     public static int getMonday(int m) {
     42         switch (m) {
     43         case 1:
     44         case 3:
     45         case 5:
     46         case 7:
     47         case 8:
     48         case 10:
     49         case 12:
     50             return 31;
     51         case 4:
     52         case 6:
     53         case 9:
     54         case 11:
     55             return 30;
     56         case 2:
     57             if (isLeapYear(year)) {
     58                 return 29;
     59             } else {
     60                 return 28;
     61             }
     62         default:
     63             return 0;
     64         }
     65     }
     66     
     67     /**
     68      * 打印月份
     69      */
     70     public static void printMonth(){
     71         
     72         for(int m = 1; m <= 12; ++m){
     73             System.out.println(m + "月" );
     74             printWeek();
     75             
     76             for(int j = 1; j <= weekDay; ++j){//按每月第一天是星期几打印相应的空格
     77                 System.out.print("     ");
     78             }
     79             
     80             int monthDay = getMonday(m);//获取每个月的天数
     81             
     82             for(int d = 1; d <= monthDay; ++d){
     83                 
     84                 System.out.printf("%-5d",d);
     85                 
     86                 weekDay = (weekDay +1) % 7;
     87                 
     88                 if(weekDay == 0){
     89                     System.out.println();
     90                 }
     91                 
     92             }
     93         System.out.println('\n');
     94 
     95         }
     96     }
     97     
     98     public static void main(String[] args) throws IOException {
     99         System.out.print("请输入一个年份:");
    100         InputStreamReader ipsr;
    101         BufferedReader bfr;
    102         
    103         ipsr = new InputStreamReader(System.in);
    104         bfr = new BufferedReader(ipsr);
    105         
    106         String s = bfr.readLine();
    107         
    108         year = Integer.parseInt(s);
    109         weekDay = firstDay(year);
    110         
    111         
    112         System.out.println("\n         " + year + " 年");
    113         printMonth();
    114     }
    115 }

    打印结果:

    请输入一个年份:2013
    
             2013 年
    1月
    ================================
    日   一    二   三   四   五    六
              1    2    3    4    5    
    6    7    8    9    10   11   12   
    13   14   15   16   17   18   19   
    20   21   22   23   24   25   26   
    27   28   29   30   31   
    
    2月
    ================================
    日   一    二   三   四   五    六
                             1    2    
    3    4    5    6    7    8    9    
    10   11   12   13   14   15   16   
    17   18   19   20   21   22   23   
    24   25   26   27   28   
    
    3月
    ================================
    日   一    二   三   四   五    六
                             1    2    
    3    4    5    6    7    8    9    
    10   11   12   13   14   15   16   
    17   18   19   20   21   22   23   
    24   25   26   27   28   29   30   
    31   
    
    4月
    ================================
    日   一    二   三   四   五    六
         1    2    3    4    5    6    
    7    8    9    10   11   12   13   
    14   15   16   17   18   19   20   
    21   22   23   24   25   26   27   
    28   29   30   
    
    5月
    ================================
    日   一    二   三   四   五    六
                   1    2    3    4    
    5    6    7    8    9    10   11   
    12   13   14   15   16   17   18   
    19   20   21   22   23   24   25   
    26   27   28   29   30   31   
    
    6月
    ================================
    日   一    二   三   四   五    六
                                  1    
    2    3    4    5    6    7    8    
    9    10   11   12   13   14   15   
    16   17   18   19   20   21   22   
    23   24   25   26   27   28   29   
    30   
    
    7月
    ================================
    日   一    二   三   四   五    六
         1    2    3    4    5    6    
    7    8    9    10   11   12   13   
    14   15   16   17   18   19   20   
    21   22   23   24   25   26   27   
    28   29   30   31   
    
    8月
    ================================
    日   一    二   三   四   五    六
                        1    2    3    
    4    5    6    7    8    9    10   
    11   12   13   14   15   16   17   
    18   19   20   21   22   23   24   
    25   26   27   28   29   30   31   
    
    
    9月
    ================================
    日   一    二   三   四   五    六
    1    2    3    4    5    6    7    
    8    9    10   11   12   13   14   
    15   16   17   18   19   20   21   
    22   23   24   25   26   27   28   
    29   30   
    
    10月
    ================================
    日   一    二   三   四   五    六
              1    2    3    4    5    
    6    7    8    9    10   11   12   
    13   14   15   16   17   18   19   
    20   21   22   23   24   25   26   
    27   28   29   30   31   
    
    11月
    ================================
    日   一    二   三   四   五    六
                             1    2    
    3    4    5    6    7    8    9    
    10   11   12   13   14   15   16   
    17   18   19   20   21   22   23   
    24   25   26   27   28   29   30   
    
    
    12月
    ================================
    日   一    二   三   四   五    六
    1    2    3    4    5    6    7    
    8    9    10   11   12   13   14   
    15   16   17   18   19   20   21   
    22   23   24   25   26   27   28   
    29   30   31   
  • 相关阅读:
    微信开发返回验证来源方式代码
    Yii 开发过程 tips
    PHP 搜索分词实现代码
    PHP 中文字符串截取
    Ubuntu16.04设置静态ip
    Linux(Ubuntu18.04)安装Chrome浏览器
    ubuntu18.04安装redis
    ubuntu18.04虚拟机安装docker
    虚拟机安装ssh,关闭防火墙
    面试送命题,你为什么从上家公司离职?(面试题总结大全)
  • 原文地址:https://www.cnblogs.com/CocoonFan/p/2964625.html
Copyright © 2020-2023  润新知