利用Calendar输出指定年份的全年日历

该博客介绍了一种使用Java的Calendar类和GregorianCalendar类来输出指定年份全年日历的方法。通过创建一个CalendarPrinter类,输入年份并逐月打印日历,包括星期和日期。代码中包含了错误检查、月份和周的处理逻辑。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 
  1. /**
  2.  * @author bzwm
  3.  * 
  4.  */
  5. import java.io.BufferedReader;
  6. import java.io.IOException;
  7. import java.io.InputStreamReader;
  8. import java.util.Calendar;
  9. import java.util.GregorianCalendar;
  10. public class CalendarTest {
  11.     public static void main(String[] args) throws IOException {
  12.         BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
  13.         System.out.print("请输入一个年份:");
  14.         String years = in.readLine();
  15.         CalendarPrinter cp = new CalendarPrinter(years);
  16.         cp.printCal();
  17.     }
  18. }
  19. class CalendarPrinter {
  20.     private int year;
  21.     private static final int monthCount = 12;
  22.     public CalendarPrinter(String years) {
  23.         if (!years.matches("//d{4}")) {
  24.             System.out.println("year that inputted is illagel.");
  25.             return;
  26.         }
  27.         year = Integer.parseInt(years);
  28.     }
  29.     public void printCal() {
  30.         // construct d as current date
  31.         GregorianCalendar gCal = new GregorianCalendar();
  32.         //set year
  33.         gCal.set(Calendar.YEAR, year);
  34.         for (int month = 0; month < monthCount; month++) {
  35.             gCal.set(Calendar.MONTH, month);
  36.             printOut(gCal);
  37.         }
  38.     }
  39.     private void printOut(Calendar cal) {
  40.         int month = cal.get(Calendar.MONTH);
  41.         // set cal to start date of the month
  42.         cal.set(Calendar.DAY_OF_MONTH, 1);
  43.         int weekday = cal.get(Calendar.DAY_OF_WEEK);
  44.         // print heading
  45.         System.out.println("Sun Mon Tue Wed Thu Fri Sat");
  46.         // indent first line of calendar
  47.         for (int i = Calendar.SUNDAY; i < weekday; i++)
  48.             System.out.print("    ");
  49.         do {
  50.             // print day
  51.             int day = cal.get(Calendar.DAY_OF_MONTH);
  52.             if (day > 0)
  53.                 System.out.print(" " + day + " ");
  54.             else
  55.                 System.out.print("  " + day + " ");
  56.             // start a new line after every Saturday
  57.             if (weekday == Calendar.SATURDAY)
  58.                 System.out.println();
  59.             // advance d to the next day
  60.             cal.add(Calendar.DAY_OF_MONTH, 1);
  61.             weekday = cal.get(Calendar.DAY_OF_WEEK);
  62.         } while (cal.get(Calendar.MONTH) == month);
  63.         // the loop exits when d is day 1 of the next month
  64.         // print final end of line if necessary
  65.         if (weekday != Calendar.SUNDAY)
  66.             System.out.println();
  67.     }
  68. }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值