java如何打印万年历

来源:互联网 发布:摄像头阅卷软件 编辑:程序博客网 时间:2024/06/05 06:33
Scanner input=new Scanner(System.in);
  System.out.println("<<<<<万年历>>>>>");
  int year=0;
  int month=0;
  System.out.print("请输入年份:");
  year=input.nextInt();
  System.out.print("请输入月份:");
  month=input.nextInt();
  //实例化一个当前的日历对象
  Calendar cd=Calendar.getInstance();
  
  //设置日历对象的年,月,日
  cd.set(Calendar.YEAR, year);
  cd.set(Calendar.MONTH, month-1);
  cd.set(Calendar.DATE, 1);
  
  //得到当前月份的最大值
  int day=cd.getActualMaximum(Calendar.DATE);
  //得到本月的第一天是星期几
  int week=cd.get(Calendar.DAY_OF_WEEK);
  int count=0;//一个计数变量
  
  System.out.println("\n\t\t"+year+"年"+month+"月\n");
  //打印日历的星期
  String strDate[]={"日\t","一\t","二\t","三\t","四\t","五\t","六\t"};
  for(int i=0;i<strDate.length;i++)
  {
   System.out.print(strDate[i]);
  }
  System.out.println("\n");
  
  //判断第一天对应的是星期几
  while(count<week-1)
  {
   System.out.print("\t");
   count++;
  }
  
  //循环打印日历
  for(int l=1;l<=day;l++,count++)
  {
   //包含前面的空格
   if(count%7==0)
   {
    System.out.println();
   }
   System.out.print(l+"\t");
  }
0 0