zoukankan      html  css  js  c++  java
  • 日历打印用java实现

      应用方法,用Java打印日历。

      1 import java.util.Scanner;
      2 
      3 /**
      4 *打印1900年之后的日历
      5 *@author:Archer-LCY
      6 *@date:2018年1月19日上午10:20:39
      7 */
      8 public class Calendal {
      9     //注意全局变量声明的位置
     10     /**用户输入的年份*/
     11     public static int year = Integer.MAX_VALUE;
     12     /**用户输入的月份*/
     13     public static int month = Integer.MAX_VALUE;
     14     /**每月的天数*/
     15     public static int [] dayofmonth= {31,28,31,30,31,30,31,31,30,31,30,31};
     16     public static void main(String[] args) {
     17         
     18         PrintCalendal();
     19     }
     20 
     21     public static void PrintCalendal() {
     22         // TODO Auto-generated method stub
     23         //1、让用户输入年月份
     24         InputYearandMonth();
     25         //2、计算1900-1-1到用户输入年份的天数
     26             //2-1计算各年的总天数
     27             //2-2计算各月的天数之和
     28         int sum=GetsumdayofYear();
     29         sum+=GetsumdayofMonth();
     30         //计算每月的一号是星期几
     31         int dayofweek=sum%7+1;
     32 
     33         //3、打印年月份(英文),打印月份的标题(星期一到星期日)
     34         PrintMonthTitle();
     35         //4、根据某年某月一号星期几,打印月历功能
     36         PrintCalendalContent(dayofweek);
     37     }
     38     /**
     39      * 根据当月一号打印月历内容
     40      * @param dayofweek    当月一号星期几
     41      */
     42     private static void PrintCalendalContent(int dayofweek) {
     43         // TODO Auto-generated method stub
     44         for(int i=0;i<dayofweek-1;i++) {
     45             System.out.print("	");
     46         }
     47         for(int i=0;i<dayofmonth[month-1];i++) {
     48             System.out.print((i+1));
     49             if((dayofweek+i)%7==0){
     50                 System.out.print("
    ");
     51                 
     52             }else
     53                 System.out.print("	");
     54         }
     55     }
     56 
     57     /**
     58      * 3、打印年月份(英文),打印月份的标题(星期一到星期日)
     59      */
     60 private static void PrintMonthTitle() {
     61         // TODO Auto-generated method stub
     62         String[] monthofname= {"January","February","March","April","May","June","July","August","September","October","November","December"};
     63         System.out.println(year+"	"+monthofname[month-1]);
     64         String[] weekdays= {"Mon","Tue","Wed","Thu","Fir","Sta","Sun"};
     65         for(int i=0;i< weekdays.length;i++) {
     66             System.out.print(weekdays[i]+"	");    
     67         }
     68         System.out.println();
     69     }
     70 
     71 /**
     72  * 计算1900-year整年的总天数
     73  * @return  sum总天数
     74  */
     75     private static int GetsumdayofYear() {
     76         // TODO Auto-generated method stub
     77         //判断是否输入年份
     78         if(year==Integer.MAX_VALUE) {//用户没有输入,重新调用
     79             System.out.println("输入错误请从新输入!");
     80             InputYearandMonth();
     81         }
     82         int sum=0;
     83         for(int i=1900;i<year;i++) {
     84             sum+=365;
     85             //判断是不是闰年
     86             if(isLeapYear(i)) {
     87                 sum++;//闰年多加一年
     88             }
     89         }
     90         return sum;
     91     }
     92     
     93     /**
     94      * 判断是不是闰年
     95      * @param year  要判断的年份
     96      * @return  是闰年返回true
     97      */
     98     private static boolean isLeapYear(int year ) {//此处一定要加参数year,表示的是传入的参数值而不是全局变量中的year值
     99         return year%400==0||year%4==0&&year%100!=0;//先算与再算或
    100     }
    101 
    102     /**
    103      * 计算月份天数
    104      * @return  sum返回year这一年月份的总天数
    105      */
    106     private static int GetsumdayofMonth() {
    107         // TODO Auto-generated method stub
    108         
    109         int sum=0;
    110         for(int i=0;i<month-1;i++) {
    111             sum+=dayofmonth[i];
    112             if(isLeapYear(year)&&month>=3) {
    113                 sum++;
    114             }
    115         }
    116         return sum;
    117     }
    118     
    119     public Calendal() {
    120         // TODO Auto-generated constructor stub
    121     }
    122     
    123     /**
    124      * 用户输入年份和月份
    125      */
    126     private static void InputYearandMonth() {
    127         // TODO Auto-generated method stub
    128         Scanner input =new Scanner(System.in);
    129         System.out.print("请输入年份:");
    130 //        int year=input.nextInt();//局部变量
    131         year=input.nextInt();
    132          System.out.print("请输入月份:");
    133          month = input.nextInt();
    134          //节省资源
    135          input.close();
    136          input=null;
    137     }
    138 }

    运行结果:

  • 相关阅读:
    phpMyAdmin导入大的sql文件
    mysql错误代码解释
    Sphinx在windows下安装使用[支持中文全文检索]
    Linux查看磁盘分区等命令和相关工具介绍
    webuploader大小限制
    常用的C#正则表达式!
    C# csv.excel导入
    日志
    微信
    winform窗口控件随窗口变化
  • 原文地址:https://www.cnblogs.com/archer-lcy/p/8316215.html
Copyright © 2011-2022 走看看