2023年日历Java
时间: 2023-07-30 16:04:47 浏览: 112
以下是使用Java编写的打印2023年日历的示例代码:
```java
import java.time.LocalDate;
import java.time.YearMonth;
public class CalendarPrinter {
public static void main(String[] args) {
int year = 2023;
// 打印每个月的日历
for (int month = 1; month <= 12; month++) {
YearMonth yearMonth = YearMonth.of(year, month);
LocalDate firstDay = yearMonth.atDay(1);
int firstDayOfWeek = firstDay.getDayOfWeek().getValue();
System.out.println("\n" + yearMonth.getMonth() + " " + year);
System.out.println("Mon Tue Wed Thu Fri Sat Sun");
// 打印每个月的日期
for (int i = 1; i < firstDayOfWeek; i++) {
System.out.print(" ");
}
for (int day = 1; day <= yearMonth.lengthOfMonth(); day++) {
System.out.printf("%-5d", day);
if ((firstDayOfWeek + day - 1) % 7 == 0) {
System.out.println();
}
}
System.out.println();
}
}
}
```
将上述代码保存为一个名为`CalendarPrinter.java`的文件,然后编译运行即可得到2023年的日历打印结果。
阅读全文