输出格式
程序清单
import java.time.*;
public class CalendarTest {
public static void main(String[] args) {
LocalDate date = LocalDate.now();
int month = date.getMonthValue();
int today = date.getDayOfMonth();
date = date.minusDays(today-1);
DayOfWeek weekday = date.getDayOfWeek();
int value = weekday.getValue();
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();
}
if (date.getDayOfWeek().getValue() != 1)System.out.println();
}
}
API Java.time.localDate 8
- static LocalTime now()
构造一个表示当前日期的对象 - static LocalTime of(int year, int month int day)
构造一个表示给定日期的对象 - int getYear()
- int getMonthValue()
- int getDayOfMonth()
得到当前日期的年月日。 - DayOfWeek getDayOfWeek
得到当前日期是星期几,返回一个DayOfWeek的实例。调用getValue()得到1~7之间的一个数分别对应星期一到星期日 - LocalDate plusDays(int n)
生成当前日期之后的几天日期 - LocalDate 生成当前日期之前的几天日期