package Chap4;
import java.time.DayOfWeek;
import java.time.LocalDate;
public class CalendarTest {
public static void main(String []args) {
// LocalDate date = new LocalDate.now();//注意没有new
/* LocalDate是一个不可变的日期时间对象,表示日期,通常被视为年月日。 也可以访问其他日期字段,例如日期,星期几和星期。*/
LocalDate date = LocalDate.now();
int month = date.getMonthValue();
int today = date.getDayOfMonth();
date = date.minusDays(today - 1);//返回此 LocalDate的副本,并减去指定的天数。即设置成当前几天的日期
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();
}
}
Java实现日历
最新推荐文章于 2024-07-05 08:14:42 发布