package core1; import java.time.LocalDate; public class D06_LocalDateUsage { //輸出當前日曆 public static void main(String[] args){ LocalDate date = LocalDate.now(); int month = date.getMonthValue(); int nowDay = date.getDayOfMonth(); //將date設置爲當前月的第一天,往前d(day-1)天 date = date.minusDays(nowDay - 1); int weekValue = date.getDayOfWeek().getValue(); System.out.println("Mon\tTue\tWed\tThu\tFri\tSat\tSun"); for(int i=1 ; i<weekValue ; i++) System.out.print(" \t"); while(date.getMonthValue() == month){ System.out.printf("%2d", date.getDayOfMonth()); System.out.printf(date.getDayOfMonth() == nowDay ? "*\t" : " \t"); if(date.getDayOfWeek().getValue() == 7) System.out.println(); date = date.plusDays(1); } } }
console輸出java
Mon Tue Wed Thu Fri Sat Sun 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24* 25 26 27 28