Count when to meet the goddess with Java-Java exercises

Practice question:
Today you met Xiao Zhang in the hospital. She is a beautiful nurse. You fell in love with her at first sight and wanted to pursue her. So I wanted to figure out her time to go to and from get off work and design a "coincidence" scene.
After many inquiries, you learned that Xiao Zhang and she has been working regularly since July 5, 2018, and never violated it. Her regular work schedule is: two night shifts, one day off, three day shifts, and one day off.
Today the company informs you that you have been dispatched for 46 days, and the return ticket will arrive at the station at 1 o'clock noon.
May I ask when will you be able to meet your goddess? (If you rest, you can't meet by chance)

Problem-solving ideas:

  •  1.先得到出差回来后的日期。
    
  •  2.根据那天日期和小张入职日期,算出之间相隔的天数
    
  •  3.根据小张上班的规律,来得到那天上班的情况。
    
  •  4.判断是否能够邂逅女神。
    
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class Doctor {
   public static void main(String[] args) throws ParseException {
       //获取今天的时间
       Calendar calendar = Calendar.getInstance();

       //计算45天之后的时间
       calendar.add(Calendar.DAY_OF_MONTH,46);

       //创建SimpleDateFormat对象,写入格式
       SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日");

       //获得时间字符串
       String time = calendar.get(Calendar.YEAR)+"年"+(calendar.get(Calendar.MONTH)+1)+"月"+calendar.get(Calendar.DAY_OF_MONTH)+"日";


       //传入当前时间和入职时间
       Date entryTime = sdf.parse("2018年7月5日");
       Date dayTime = sdf.parse(time);//2020-12-12

       //求出间隔时间
       long d1 = ((dayTime.getTime()-entryTime.getTime())/1000/60/60/24);


       //小张上班的规律是7天一轮回,间隔时间对七取余
       int x = (int) d1%7 ;
       System.out.println(x);

       //通过判断x值,来确定当天小张的上班不能情况
       switch (x){
           case 1://夜班
           case 2://夜班
           case 4://白班
           case 5://白班
           case 6://白班
               System.out.println("邂逅女神日期是:"+time);
               break;
           case 3://休息
           case 0://休息
               time = calendar.get(Calendar.YEAR)+"年"+(calendar.get(Calendar.MONTH)+1)+"月"+(calendar.get(Calendar.DAY_OF_MONTH)+1)+"日";
               System.out.println("邂逅女神日期是:"+time);
       }
   }
}

Guess you like

Origin blog.csdn.net/LinKin_Sheep/article/details/109325743