java.time.LocalDate
- static LocalTime now()
构造一个表示当前日期的对象
- static LocalTime of(int year, int month, int day)
构造一个表示给定日期的对象
- int getYear()
- int getMonthValue()
- int getDayOfMonth()
的到当前日期的年、月和日
- DayOfWeek getDayOfWeek()
得到当前日期是星期几,作为DayOfWeek类的一个实例返回。调用 getValue() 来得到 1~7 之间的一个数,表示这是星期几,1表示星期一,7表示星期日、
- LocalDate plusDays(int n)
- LocalDate minusDays(int n)
生成当前日期之后或之前n天的日期
例子:
使用 LocalDate 编写一个日历程序
/**
* 使用LocalDate编写一个日历程序
*/
public static void calendar(){
LocalDate date = LocalDate.now();
//获取当前的月和日
int month = date.getMonthValue();
int today = date.getDayOfMonth();
//设置为这个月的第一天,并得到这一天为星期几
date = date.minusDays(today - 1);
DayOfWeek dayOfWeek = date.getDayOfWeek();
int value = dayOfWeek.getValue();
System.out.println("Mon Tue Web Thu Fri Sat Sun");
for (int i = 0; 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();
}
}
if (date.getDayOfWeek().getValue() != 1){
System.out.println();
}
}