java打印日历calendar
时间: 2023-11-12 20:58:08 浏览: 100
好的,以下是Java打印日历的代码:
```java
import java.util.Calendar;
public class PrintCalendar {
public static void main(String[] args) {
// 获取当前时间
Calendar calendar = Calendar.getInstance();
int year = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH) + 1;
int day = calendar.get(Calendar.DATE);
// 打印日历
System.out.println("日\t一\t二\t三\t四\t五\t六");
calendar.set(year, month - 1, 1);
int weekDay = calendar.get(Calendar.DAY_OF_WEEK) - 1;
for (int i = 0; i < weekDay; i++) {
System.out.print("\t");
}
int maxDay = calendar.getActualMaximum(Calendar.DATE);
for (int i = 1; i <= maxDay; i++) {
System.out.print(i + "\t");
if ((weekDay + i) % 7 == 0) {
System.out.println();
}
}
}
}
```
阅读全文