可以获取某年的指定月份或者是全年的日历
package rili; //1800.1.1周三 public class Calendar { int year,month; public void Monthtop(){ System.out.println("Sun Mon Tues Wed Thur Fri sat"); } public Calendar(int y,int m){ year=y; month=m; } public Calendar(int y){ year=y; } public int FirstDayofMonth(int m){ int totaldays=0; for(int i=1800;i<year;i++){ if(isLeapYear(i)){ totaldays+=366; }else{ totaldays+=365; } } for(int i=1;i<m;i++){ totaldays+=DaysofMonth(year,i); }totaldays+=3; return (totaldays%7); } public void printLine(){ System.out.println(); System.out.println("____________________________________________________"); } public void Allyear(){ for(int i=1;i<=12;i++){ printMonth(i); } } public void printMonth(int m){//打印指定月份 Monthtop(); int day=1,cnt=0; for(int i=1;i<=FirstDayofMonth(m);i++){//打印第一行的空格数 System.out.print(" "); } for(int i=1;i<=7-FirstDayofMonth(m);i++){//打印第一行显示的数字 System.out.print(day+" "); day++; }System.out.println(); while(day<=DaysofMonth(year,m)){ System.out.print(day+" "); day++;cnt++; if(cnt==7){ System.out.println(); cnt=0; } }printLine(); } public boolean isLeapYear(int y){ if(y%400==0||(y%4==0&&y%100!=0)){ return true; }return false; } public int DaysofMonth(int y,int m){ int []days=new int [13]; days[0]=0; days[1]=31; days[3]=31; days[5]=31; days[7]=31; days[8]=31; days[10]=31; days[12]=31; days[2]=28; days[4]=30; days[6]=30; days[9]=30; days[11]=30; if(isLeapYear(y)){ days[2]=29; } return days[m]; } public static void main(String[] args) { // TODO Auto-generated method stub Calendar c =new Calendar(2019,10); c.Allyear(); } }