import java.time.*; /** * @version 1.5 2015-05-08 * @author Cay Horstmann */ public class ClaendarTest { public static void main(String[] args) { LocalDate date = LocalDate.now();//构建此对象时默认为该年该月1号 int month = date.getMonthValue(); int today = date.getDayOfMonth(); date = date.minusDays(today - 1); // 将date设置为这个月的第一天 DayOfWeek weekday = date.getDayOfWeek();//获取第一天date是星期几 int value = weekday.getValue(); // 将星期以数值形式表达:1 = Monday, . . . , 7 = Sunday System.out.println("Mon Tue Wed Thu Fri Sat Sun"); for (int i = 1; i < value; i++)//循环打印日历前的缩进 System.out.print(" "); 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(); //判断date是否为星期1,若为星期一则换行(为了日历的格式) } if (date.getDayOfWeek().getValue() != 1) System.out.println(); } }