zoukankan      html  css  js  c++  java
  • Java万年历,输入年月获取该年月日历表

      1 //输入年份和月份,打印出这个月的日历表
      2 /*
      3     1.1900年1月1日是星期一
      4     2.计算输入的年份距离1900年有多少天再计算当年1月1日距这个月有多少天
      5         1)
      6     3.总天数%7得出从星期几开始
      7     注:计算机中的时间最小到1900年,此外UNIX系统认为1970年1月1日0点是时间纪元。
      8     so,在本程序中不考了1900年以前的年份了。有兴趣的可以自己研究下。
      9 */
     10 import java.util.Scanner;
     11 class Calender{
     12 
     13     public static void main(String[] args){
     14         
     15         print();
     16 
     17     }
     18 
     19     //打印输出
     20     public static void print(){
     21         Scanner sc = new Scanner(System.in);
     22         System.out.println("请输入年份:");
     23         int year = sc.nextInt();
     24         System.out.println("请输入月份(1~12):");
     25         int month = sc.nextInt();
     26 
     27         int days = getDays(year, month);//getDays方法详细请往下看
     28         //days+1:day是总天数,输入月份的总天数只是这个月之前的天数,
     29         //加上1变为这个月开始的第一天
     30         int week = days%7==0?1:days%7+1;//开始的第一天是星期几
     31         System.out.println("日	一	二	三	四	五	六");
     32         
     33         //输出第一行(第一个星期)空出来部分
     34         for(int i=1; i<=week; i++){
     35             System.out.print(" 	");
     36         }
     37 
     38         //输出第一行(第一个星期)各天
     39         for(int i=1; i<=7-week; i++){
     40             System.out.print(i+"	");
     41         }
     42 
     43         System.out.println();
     44 
     45         //1~12月的个月天数
     46         int monthDay = 0;
     47         switch(month){
     48                 case 2:
     49                     if(year%4==0&&year%100!=0 || year%400==0 ){
     50                         monthDay=29;
     51                     }else{
     52                         monthDay=28;
     53                     }
     54                     break;
     55 
     56                 case 4:
     57                 case 6:
     58                 case 9:
     59                 case 11:
     60                     monthDay=30;
     61                     break;
     62 
     63                 default :
     64                     monthDay=31;
     65                     break;
     66             }
     67 
     68         //输出剩下的日期,从第二周开始了,所以是8-week
     69         for(int i=8-week; i<=monthDay; i++){
     70     
     71             System.out.print(i+"	");
     72             
     73             //每七天换一行则当日期能整除7就换行
     74             if((i+week)%7==0){
     75                 System.out.println();
     76             }
     77         }
     78 
     79 
     80     }
     81 
     82     /*
     83         计算当年当月的距1900年1.1的总天数
     84     */
     85     public static int getDays(int year, int month){
     86         //判断这年是闰年或者平年,得到年的总天数
     87         int day1=0, day2=0;
     88 
     89         for(int i=1900; i<year; i++){
     90             if(i%4==0&&i%100!=0 || i%400==0){
     91                 day1+=366;
     92             }else{
     93                 day1+=365;
     94             }
     95         }
     96     
     97         //得到月的总天数
     98         for(int i=1; i<month; i++){
     99             switch(i){
    100                 case 2:
    101                     if(year%4==0&&year%100!=0 || year%400==0 ){
    102                         day2+=29;
    103                     }else{
    104                         day2+=28;
    105                     }
    106                     break;
    107 
    108                 case 4:
    109                 case 6:
    110                 case 9:
    111                 case 11:
    112                     day2+=30;
    113                     break;
    114 
    115                 default :
    116                     day2+=31;
    117                     break;
    118             }
    119         }
    120     
    121         return day1+day2;
    122 
    123     }
    124     
    125 }
  • 相关阅读:
    [LeetCode] 143. 重排链表
    [LeetCode] 342. 4的幂
    [LeetCode] 1744. 你能在你最喜欢的那天吃到你最喜欢的糖果吗?
    [LeetCode] 148. 排序链表
    [LeetCode] 525. 连续数组
    [LeetCode] 160. 相交链表
    [LeetCode] 134. 加油站
    [LeetCode] 474. 一和零
    CentOS 升级 OpenSSH
    AWS 证书取消挂靠
  • 原文地址:https://www.cnblogs.com/xinge1993/p/4663015.html
Copyright © 2011-2022 走看看