package test; import java.time.DayOfWeek; import java.time.LocalDate; /** * * @ClassName: CalendarTest * @Description: 用来打印当月的日历 * @author William_Dai * @date 2019年5月21日 * */ public class CalendarTest { public static void main(String[] args) { LocalDate date = LocalDate.now(); //System.out.println(date); // 输出结果: 2019-05-21 int month = date.getMonthValue(); int today = date.getDayOfMonth(); //System.out.println(month +"***"+today ); // 输出结果: 5***21 date = date.minusDays(today - 1); //设置为一个月的第一天 : 2019-05-01 //date = date.minusDays(today); // 2019-04-10 //System.out.println(date); DayOfWeek weekday = date.getDayOfWeek(); //System.out.println(weekday); // WEDNESDAY int value = weekday.getValue(); //System.out.println(value); // 输出: 3 (the day-of-week, from 1 (Monday) to 7 (Sunday)) System.out.println("Mon Tue Wed Thu Fri Sat Sun"); for(int i = 1 ; i < value; i ++){ System.out.print(" "); } //System.out.println(date.getMonthValue());//5 //System.out.println(month); //5 //System.out.print(date.getDayOfMonth()); // 1 //System.out.printf("%3d",date.getDayOfMonth()); //date = date.plusDays(1); // 加一天 //System.out.printf("%3d",date.getDayOfMonth()); while(date.getMonthValue() == month){ System.out.printf("%3d",date.getDayOfMonth()); if(date.getDayOfMonth() == today){ System.out.print("*"); }else{ System.out.print(" "); } date = date.plusDays(1); if(date.getDayOfWeek().getValue() == 1){ System.out.println(); } } //System.out.println(date.getDayOfWeek().getValue()); if(date.getDayOfWeek().getValue() != 1){ System.out.println(); } } }
输出结果是:当天日期是 21号。