1 //2013-10-7 2 import java.util.*; 3 import java.text.DateFormatSymbols; 4 public class dateprint{ 5 public static void main(String[] args){ 6 int year,month,day; 7 String input; 8 Scanner in=new Scanner(System.in); 9 input=in.nextLine(); 10 year=Integer.parseInt(input.substring(0,4)); 11 month=Integer.parseInt(input.substring(5,7)); 12 day=Integer.parseInt(input.substring(8)); 13 PRINT a=new PRINT(); 14 a.Monthprint(year,month,day); 15 16 } 17 } 18 19 class PRINT{ 20 public void Monthprint(int y,int m,int D){ 21 GregorianCalendar d=new GregorianCalendar(y,m,D); 22 23 int today=d.get(Calendar.DAY_OF_MONTH); 24 int month=d.get(Calendar.MONTH); 25 26 d.set(Calendar.DAY_OF_MONTH,1); //将d设为该月的第一天 27 28 int weekday=d.get(Calendar.DAY_OF_WEEK); //得到d是星期几 29 int firstdayofweek=d.getFirstDayOfWeek(); 30 31 int indent=0; //判断第一周前面有几个空 32 while(weekday!=firstdayofweek){ 33 indent++; 34 d.add(Calendar.DAY_OF_WEEK,-1); 35 weekday=d.get(Calendar.DAY_OF_WEEK); 36 } 37 38 //打印weekday names 39 String[] weekdayname=new DateFormatSymbols().getShortWeekdays(); 40 do{ 41 System.out.printf("%4s",weekdayname[weekday]); 42 d.add(Calendar.DAY_OF_MONTH,1); 43 weekday=d.get(Calendar.DAY_OF_WEEK); 44 }while(weekday!=firstdayofweek); 45 46 System.out.println(); 47 48 for(int i=1;i<=indent;i++) 49 System.out.print(" "); 50 d.set(Calendar.DAY_OF_MONTH,1); 51 52 do{ 53 int day=d.get(Calendar.DAY_OF_MONTH); 54 System.out.printf("%3d",day); 55 56 if(day==today) System.out.print("*"); 57 else System.out.print(" "); 58 59 d.add(Calendar.DAY_OF_MONTH,1); 60 weekday=d.get(Calendar.DAY_OF_WEEK); 61 62 if(weekday==firstdayofweek) System.out.println(); 63 }while(d.get(Calendar.MONTH)==month); 64 65 if(weekday!=firstdayofweek) System.out.println(); 66 67 } 68 }