需求:找到休息日
某林业工人孙工,作息规律为上三天班,休息一天,经常不确定休息日是否是周末。为此,请你开发一个程序,当孙工 输入年以及月,以日历方式显示对应月份的休息日,用中括号进行标记(可以查看以前的休息情况和将来的休息情 况)。同时,统计出本月有几天休息,轮到周末休息有几天。(注:首次休息日是2020年2月1日)
业务逻辑代码:
public class Demo {
/*
* 找到休息日
*/
@SuppressWarnings("resource")
public static void main(String[] args) throws ParseException {
Scanner input = new Scanner(System.in);
SimpleDateFormat f = new SimpleDateFormat("yyyy年MM月dd日");
Date data1 = new Date();
Date data2 = new Date();
Calendar c1 =Calendar.getInstance();
System.out.print("请输入年:");
int year = input.nextInt();
System.out.print("请输入月:");
int month = input.nextInt();
data1 = f.parse(year+"年"+month+"月"+"01日");
c1.setTime(data1);
data2 = f.parse("2020年02月01日");//首次休息日
System.out.println("星期日 星期一 星期二 星期三 星期四 星期五 星期六");
int restDay = 0;//休息天数
int weekendRestDay = 0;//周末休息天数
long time = data2.getTime();
for(int i=1;i<=c1.getActualMaximum(Calendar.DAY_OF_MONTH);i++) {
if(i==1) {
switch(c1.get(Calendar.DAY_OF_WEEK)) {
case 2:
System.out.print("\t");
break;
case 3:
System.out.print("\t\t");
break;
case 4:
System.out.print("\t\t\t");
break;
case 5:
System.out.print("\t\t\t\t");
break;
case 6:
System.out.print("\t\t\t\t\t");
break;
case 7:
System.out.print("\t\t\t\t\t\t");
break;
}
}
c1.set(Calendar.DAY_OF_MONTH,i);
if((c1.getTime().getTime()-time)%(4*24*60*60*1000)==0) {
System.out.print("["+i+"]"+"\t");
restDay++;
if(c1.get(Calendar.DAY_OF_WEEK)==7 || c1.get(Calendar.DAY_OF_WEEK)==1) {
weekendRestDay++;
}
}else {
System.out.print(i+"\t");
}
if(c1.get(Calendar.DAY_OF_WEEK)==7) {
System.out.println();
}
}
System.out.println();
System.out.println("本月休息天数有:"+restDay+"天");
System.out.println("本月轮到周末休息天数是"+weekendRestDay+"天");
}
}