用java显示日历

此程序提示用户输入年份和代表该年第一天是星期几的数字,然后调用相应方法在控制台上显示该年的日历表。例如,用户输入年份2013和代表201311日为星期二的2,程序显示该年每个月的月历。

程序分为MyCalendar.java,ShowCalendar.java

MyCalendar.java

代码详情:

package km;


public class MyCalendar {
    int year;
    int firstDayOfYear;
    int [] firstDayOfMonth;
    int [] DayAmountOfMonth;
    public MyCalendar(){
    this.year=2016;
    this.firstDayOfYear=4;
    this.firstDayOfMonth= computerDayAmountOfMonth();
    this.DayAmountOfMonth=(int[])computerDayAmountOfMonth();
    }
    public MyCalendar(int year,int fistDayOfYear){
super();
this.year = year;
this.firstDayOfYear = firstDayOfYear;
this.firstDayOfMonth = computerDayAmountOfMonth();
this.DayAmountOfMonth = (int[])computerDayAmountOfMonth();
}
    public boolean isleafYear(int year){
if(year%4==0){
if(year%100==0){
if(year%400==0){
return true;
}
return false;
}
return false;
}
return false;
    }
    private int[ ] computerDayAmountOfMonth(){
      int[] dayAmountOfMonth = {0,31,28,31,30,31,30,31,31,30,31,30,31};
      int[] dayAmountOfMonthLeaf = {0,31,29,31,30,31,30,31,31,30,31,30,31};
      if(isleafYear(year))
      return dayAmountOfMonthLeaf;
      else
      return dayAmountOfMonth;
    }
    private int[] computerdayAmountOfMonth(){
int[] firstDayOfMonth = new int[13];
firstDayOfMonth[1] = this.firstDayOfYear;
for(int month = 2;month<=12;month++)
if((month==2)||(month==4)||(month==6)||(month==8)||(month==9)||(month==11))
{firstDayOfMonth[month]=(firstDayOfMonth[month-1]+31)%7;
}
else if(month==3){
if (isleafYear(year))
firstDayOfMonth[month]=(firstDayOfMonth[month-1]+29)%7;
else
firstDayOfMonth[month]=(firstDayOfMonth[month-1]+28)%7;
}
else firstDayOfMonth[month]=(firstDayOfMonth[month-1]+30)%7;
return  firstDayOfMonth;
}


    public void printAmount(int month){
System.out.println("\n\t\t\t" + month + "月");
System.out.println("星期天\t星期一\t星期二\t星期三\t星期四\t星期五\t星期六" );
for(int i=0;i<=firstDayOfMonth[month];i++){
System.out.print("\t");
for(i=1;i<=DayAmountOfMonth[month];i++)
{
System.out.print(i+"\t");
if((i+firstDayOfMonth[month])%7==0)
{
System.out.println();
}


        }
        }
    }
  }

ShowCalendar.java

代码详情

package km;


import java.util.Scanner;


public class ShowCalendar {



public static void main(String[] args) {
Scanner year=new Scanner(System.in);
System.out.print("请输入年份:");
int nowyear=year.nextInt();
System.out.print("请输入第一天是星期几的数字:");
int nowday=year.nextInt();
MyCalendar myCalenar=new MyCalendar(nowyear,nowday);
System.out.println("\t\t\t"+nowyear+"年");
for(int i=1;i<13;i++)
{
myCalenar.printAmount(i);
if(i!=12)
System.out.println();
}
 }



}


你可能感兴趣的:(用java显示日历)