public class Demo02 {
// 2.给定任意一个日期,使用LocalDate显示出当月的日历,效果图如下。
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入您要查询的年份:");
int year = scanner.nextInt();
System.out.println("请输入您要查询的月份:");
int month = scanner.nextInt();
System.out.println("请输入您要查询的日:");
int day = scanner.nextInt();
LocalDate localDate = LocalDate.of(year, month, day);
System.out.println("日\t一\t二\t三\t四\t五\t六\t");
//获取最后一天
int lastDay = localDate.with(TemporalAdjusters.lastDayOfMonth()).getDayOfMonth();
//获取第一天是星期几
int week = localDate.withDayOfMonth(1).getDayOfWeek().getValue();
for (int i = 0; i < week; i++) {//打印第一行的空格
System.out.print("\t");
}
if (week==7) { //第一个为7时加一行空格
System.out.println();
}
for (int i = 1; i <= lastDay; i++) {
if(i==day) {
System.out.print(i+"*\t"); //输出天数
}else {
System.out.print(i+"\t"); //输出天数
}
if ((i+week)%7==0) { //打到7的倍数的时候就换行
System.out.println();
}
}
}