LocalDate类的使用
1 /** 2 * 让我们手写一个日历吧 3 * @param args 4 */ 5 public static void main(String[] args) { 6 LocalDate date = LocalDate.now(); 7 int month = date.getMonthValue(); 8 int today = date.getDayOfMonth(); 9 10 date = date.minusDays(today - 1); // Set to start of month 11 DayOfWeek weekday = date.getDayOfWeek(); 12 int value = weekday.getValue(); // 1 = Monday ...7 = Sunday 13 14 System.out.println("Mon Tue Mon Thu Fri Sat Sun"); 15 for(int i = 1; i < value; i ++) 16 System.out.print(" "); 17 while (date.getMonthValue() == month) 18 { 19 System.out.printf("%3d", date.getDayOfMonth()); 20 if (date.getDayOfMonth() == today) 21 System.out.print("*"); 22 else 23 System.out.print(" "); 24 date = date.plusDays(1); 25 if (date.getDayOfWeek().getValue() == 1) System.out.println(); 26 } 27 if (date.getDayOfWeek().getValue() != 1) { 28 System.out.println(); 29 } 30 }
显示效果:
原文:https://www.cnblogs.com/Night-Watch/p/12099133.html