fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10. static Scanner sc=new Scanner(System.in);
  11.  
  12. static int year;
  13. static int month;
  14. public static void main(String[] args){
  15. System.out.println("请输入年份:");
  16. String ye=sc.next();
  17. while(true){
  18. if(ye.matches("\\d{4}")){
  19. year=Integer.parseInt(ye);
  20. break;
  21. }else{
  22. System.out.println("年份输入错误,请重新输入");
  23. }
  24. }
  25.  
  26. System.out.println("请输入月份:");
  27. String mon=sc.next();
  28. while(true){
  29. if(mon.matches("[1-9]|0[1-9]|1[012]")){
  30. month=Integer.parseInt(mon);
  31. break;
  32. }else{
  33. System.out.println("月份输入错误,请重新输入");
  34. }
  35. }
  36.  
  37.  
  38. Calendar cal=Calendar.getInstance();
  39. cal.set(year, month-1, 1);
  40.  
  41. int day=cal.getActualMaximum(Calendar.DATE);
  42. int index=cal.get(Calendar.DAY_OF_WEEK); //判断该月第一天是星期几,输出几个空格
  43.  
  44. System.out.println("日"+"\t"+"一"+"\t"+"二"+"\t"+"三"+"\t"+"四"+"\t"+"五"+"\t"+"六"+"\t");
  45. for(int i=0;i<index-1;i++){
  46. System.out.print(" "+"\t");
  47. }
  48.  
  49. for(int i=1;i<=day;i++){ //输出该月每天,输出七个换行
  50. System.out.print(i+"\t");
  51. if((index-1+i)%7==0){
  52. System.out.println("");
  53. }
  54. }
  55. }
  56. }
Success #stdin #stdout 0.13s 30036KB
stdin
2018
5
stdout
请输入年份:
请输入月份:
日	一	二	三	四	五	六	
 	 	1	2	3	4	5	
6	7	8	9	10	11	12	
13	14	15	16	17	18	19	
20	21	22	23	24	25	26	
27	28	29	30	31