使用java LocalDate做一个日历并标注当天的日期
效果图
代码
LocalDate now = LocalDate.now(); // 获取当前时间 日月 int month = now.getMonthValue(); int today = now.getDayOfMonth(); // 月初 LocalDate localDate = now.minusDays(today-1); // 月初第一天是星期几 DayOfWeek dayOfWeek = localDate.getDayOfWeek(); int value = dayOfWeek.getValue(); System.out.println("Mon Tue Wed Thu Fri Sat Sun"); for (int i = 0; i < value; i++) { System.out.print(" "); } // 只要还是当月就一直循环 while (localDate.getMonthValue() == month){ // 打印当前天数 System.out.printf("%3d", localDate.getDayOfMonth()); // 如果是当天则拼接’*‘ if (localDate.getDayOfMonth() == today){ System.out.print("*"); }else { System.out.print(" "); } // 进入下一天 localDate = localDate.plusDays(1L); // 到下一个星期一开始换行 if (localDate.getDayOfWeek().getValue() == 1){ System.out.println(); } }