calender类java
时间: 2023-06-14 16:04:53 浏览: 113
下面是一个简单的Java Calendar类例子,它可以打印出指定年份的每个月份的日历:
```
import java.util.*;
public class CalendarExample {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("请输入年份:");
int year = scanner.nextInt();
for (int month = 0; month < 12; month++) {
Calendar calendar = Calendar.getInstance();
calendar.set(year, month, 1);
int daysInMonth = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
int startDay = calendar.get(Calendar.DAY_OF_WEEK);
System.out.println(month + 1 + "月 " + year);
System.out.println("日 一 二 三 四 五 六");
for (int i = 1; i < startDay; i++) {
System.out.print(" ");
}
for (int day = 1; day <= daysInMonth; day++) {
System.out.printf("%2d ", day);
if ((day + startDay - 1) % 7 == 0) {
System.out.println();
}
}
System.out.println("\n");
}
}
}
```
运行该例子,输入年份后,将会打印出该年份的每个月份的日历。
阅读全文