【Java】LocalDate类编写一个日历程序
1 import java.time.*; 2 3 public class CalendarTest { 4 public static void main(String[] args) { 5 LocalDate date = LocalDate.now(); 6 int month = date.getMonthValue(); 7 int today = date.getDayOfMonth(); 8 9 date = date.minusDays(today-1); 10 DayOfWeek weekday = date.getDayOfWeek(); 11 int value = weekday.getValue(); 12 13 System.out.println("Mon Tue Wed Thu Fri Sat Sun"); 14 for(int i = 1;i< value;i++) 15 System.out.println(" "); 16 while(date.getMonthValue() == month) 17 { 18 System.out.printf("%3d",date.getDayOfMonth()); 19 if(date.getDayOfMonth()==today) 20 System.out.printf("*"); 21 else 22 System.out.printf(" "); 23 date = date.plusDays(1); 24 if(date.getDayOfWeek().getValue() == 1) System.out.println(); 25 } 26 if(date.getDayOfWeek().getValue() !=1 ) System.out.println(); 27 } 28 }
利用LocalDate类编写一个日历程序,可处理星期几以及各月天数不同等问题
我们不需要了解LocalDate类如何计算类,只需要使用这个类的借口就可以了。