某公司软件开发工程师孙工,作息规律为上三天班,休息一天,经常不确定休息日 是否周末,为此,请你开发一个程序,当孙工输入年及月,以日历方式显示对应月 份的休息日,用中括号进行标记.同时,统计出本月有几天休息,轮到周末休息有 几天.(注:首次休息日是 2019 年 2 月 2 日)
public class FindRestday {
public static void main(String[] args) {
while (true) {
Scanner input = new Scanner(System.in);
System.out.println("请输入年:");
int year = input.nextInt();
System.out.println("请输入月:");
int month = input.nextInt()-1;
Calendar cal = Calendar.getInstance();
cal.set(Calendar.YEAR, year);
cal.set(Calendar.MONTH, month);
int maxDay = cal.getActualMaximum(Calendar.DATE);
cal.set(Calendar.DATE, 1);
int firstDay = cal.get(Calendar.DAY_OF_WEEK);
cal.set(year, month, maxDay, 0, 0, 0);
Date endDate = cal.getTime();
cal.set(2019, 1, 2, 0, 0, 0);
Date startDate = cal.getTime();
long betweenDate = (endDate.getTime() - startDate.getTime())/(60*60*24*1000);
cal.set(year, month, 1, 0, 0, 0);
Date endDate2 = cal.getTime();
long betweenDate2 = (endDate2.getTime() - startDate.getTime())/(60*60*24*1000);
int blankNum = firstDay-1;
System.out.println("星期日\t星期一\t星期二\t星期三\t星期四\t星期五\t星期六\t");
for (int j = 0; j < blankNum; j++) {
System.out.print("\t");
}
int count = 0;
int[] restDay = new int[maxDay];
int js=0;
for (int d = 0; d < betweenDate; d++) {
int rest = (int) (d-betweenDate2+1);
if (d>=betweenDate2 && d%4==0) {
count++;
restDay[rest-1] = rest;
}
}
for (int i = 1; i <= maxDay; i++) {
if(i==restDay[i-1]) {
System.out.print("["+i+"]"+"\t");
if((i+blankNum)%7==0 || (i+blankNum)%7==1) {
js++;
}
} else {
System.out.print(i+"\t");
}
if((i+blankNum)%7==0) System.out.println();
}
System.out.println();
System.out.println("本月休息天数有:"+count);
System.out.println("本月轮到周末休息天数是:"+js+"天");
System.out.println("==============================================================");
System.out.println("是否继续?:1.是 2.否");
int isContinue = input.nextInt();
if(isContinue==2) break;
}
}
}