日历表
import java.time.DayOfWeek;
import java.time.LocalDate;
public class Demo {
public static void main(String[] args) {
//(静态工厂方法)构造一个表示给定日期的对象
//LocalDate date = LocalDate.of(2021,9,7);
//构造一个表示当前日期的对象
LocalDate date = LocalDate.now();
//得到当前日期的年
int year = date.getYear();
//得到当前日期的月
int month = date.getMonthValue();
//得到当前日期的日
int today = date.getDayOfMonth();
//生成当前日期之前的today-1天
date = date.minusDays(today - 1);
//得到当前日期是星期几,作为一个DayOfWeek的实例返回。
DayOfWeek weekday = date.getDayOfWeek();
//得到星期的值,1表示星期一,以此类推。
int value = weekday.getValue();
System.out.println("Tis is " + year);
System.out.println("Mon Tue Wed Thu Fri Sat Sun");
for (int i = 1; i < value; i++)
System.out.print(" ");
while (date.getMonthValue() == month){
//%3d表示整数占用3个单位
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();
}
}
输出:
Tis is 2021
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 29
30 31