首页 > 代码库 > JAVA语言程序设计(基础篇)第10版 13.4

JAVA语言程序设计(基础篇)第10版 13.4

import java.util.*;

public class Exercise13_04 {

 public static void main(String[] args) {

  Scanner input = new Scanner(System.in);   

System.out.println("输入月份和年份:");

  PrintCalendar Y = new PrintCalendar(input.nextInt() , input.nextInt());

  Y.printMonthTitle();   Y.printMonthBody();

 }

}

 class PrintCalendar {  

Calendar value;    

public PrintCalendar(int Month , int Year)   {    value = http://www.mamicode.com/new GregorianCalendar( Year , Month , 1); }

public void printMonthTitle()   {      

System.out.println("     " + getMonthName(value.get(Calendar.MONTH)) + " " + value.get(Calendar.YEAR));   

System.out.println("----------------------------------");   

System.out.println(" Sun Mon Tue Wed Thu Fri Sat");   }

  public String getMonthName(int month)

{    String monthName = "";   

switch (month)

{   

case 1: monthName = "January"; break;  

  case 2: monthName = "February"; break;   

case 3: monthName = "March"; break;  

  case 4: monthName = "April"; break;  

  case 5: monthName = "May"; break;   

case 6: monthName = "June"; break;   

case 7: monthName = "July"; break;   

case 8: monthName = "August"; break;  

  case 9: monthName = "September"; break;  

  case 10: monthName = "October"; break;   

case 11: monthName = "November"; break;   

case 12: monthName = "December";   

}   

return monthName;

  }  

public void printMonthBody()

  {

   int startDay = value.get(Calendar.DAY_OF_WEEK)-1;   

int numberOfDaysInMonth = value.getActualMaximum(Calendar.DAY_OF_MONTH);  

  int i = 0;    for(i = 0; i < startDay ; i++  )     System.out.print("    ");   

for(i = 1 ; i <= numberOfDaysInMonth; i++  )

{    

System.out.printf("%4d",i);   

 if((i + startDay) % 7 == 0)    

System.out.println();   

}   

System.out.println();  

}  

    }

 

JAVA语言程序设计(基础篇)第10版 13.4