import java.util.Calendar; import java.util.Scanner; public class test3 { public static void main(String[] args) { Scanner scanner=new Scanner(System.in); System.out.println("请输入年份和月份,以,隔开"); String s=scanner.next(); String[]s1=s.split(","); Calendar calendar=Calendar.getInstance(); // 类型转换 int year=new Integer(s1[0]); int month=new Integer(s1[1]); // 月份从零开始 calendar.set(year ,month-1,1 ); int one= calendar.get(Calendar.DAY_OF_WEEK); // calendar.getActualMaximum(Calendar.DAY_OF_MONTH)算出本月最多有几天 // c.getActualMaximum(Calendar.WEEK_OF_MONTH)几周 int days= calendar.getActualMaximum(Calendar.DAY_OF_MONTH); // 打印星期文字 Object[] obj = {"日","一","二","三","四","五","六"}; for (int i = 0; i < obj.length; i++) { System.out.print(obj[i]+" "); } System.out.println(); // 空出的前几天打印空格 for (int i = 1; i <one ; i++) { System.out.print(" "); } // 打印日期,注意one+i-1 for (int i = 1; i <=days ; i++) { System.out.printf("%2d ",i); if((one+i-1)%7==0) { System.out.println(); System.out.println(); } } } }