package cn.zcc.esay;
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.Month;
/**
* @author kiryul
* @version 1.0.0
* @ClassName test4_calendar.java
* @createTime 2020年04月25日 20:55:00
* @Description 日历
*/
public class test4_calendar {
public static void main(String[] args) {
final String[] WEEKNAME={"星期一","星期二","星期三","星期四","星期五","星期六","星期天"};
LocalDate date=LocalDate.now();
int yearNum=date.getYear();
int monthNum=date.getMonthValue();
int dayNum=date.getDayOfMonth();
DayOfWeek week=date.getDayOfWeek();
Month month=date.getMonth();
//判断是否为闰年
boolean leap=date.isLeapYear();
//获取当月的天数
int monthdays=month.length(leap);
LocalDate first=date.withDayOfMonth(1);
int head=first.getDayOfWeek().getValue();
System.out.println("*****************************************************************");
System.out.println("Today:"+yearNum+"-"+monthNum+"-"+dayNum+" "+week.name());
System.out.println("*****************************************************************");
System.out.println(month.name());
System.out.println("-----------------------------------------------------------------");
for (int i = 0; i < WEEKNAME.length; i++) {
System.out.print(WEEKNAME[i]+"|");
}
System.out.println();
int i=1;
while (i<monthdays){
for (int j = 1; j <= 7; j++) {
if(head!=j) System.out.print(" ");
else {
if(i>monthdays) break;
if(i==dayNum) System.out.print("*");
if(i<10) System.out.print(" "+i+" ");
else System.out.print(" "+i+" ");
i++;
head++;
}
}
head=1;
System.out.println();
}
}
}
输出:
*****************************************************************
Today:2020-4-25 SATURDAY
*****************************************************************
APRIL
-----------------------------------------------------------------
星期一|星期二|星期三|星期四|星期五|星期六|星期天|
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
Process finished with exit code 0