根据员工入职时间和合同期计算下一次合同签订时间

来源:互联网 时间:1970-01-01

 1 import java.text.SimpleDateFormat;

2 import java.util.Calendar;

3 import java.util.Date;

4 import java.util.GregorianCalendar;

5 import java.util.Scanner;

6

7 public class ContractDate { 8
9 /**
10 * @param args
11 * 2 根据员工入职时间和合同期计算下一次合同签订时间
12 * a 入职时间为指定格式字符串
13 * b 合同期单位为月
14 * c下次合同签订时间为到期前一个月份的工作日(遇到周六,周日顺延到下周一)
15 * d 返回值为指定格式的字符串
16 * @throws Exception
17 */
18 public static void main(String[] args) throws Exception {
19 // TODO Auto-generated method stub
20 Scanner console = new Scanner(System.in);
21 System.out.println("请输入入职时间:");
22 String str = console.nextLine();
23
24 Scanner sc = new Scanner(System.in);
25 System.out.println("请输入合同期(单位:月):");
26 int contractMonths = sc.nextInt();
27 String exp = agreement(str, contractMonths);
28 System.out.println("签订日期为:" + exp);
29 }
30
31 public static String agreement(String str, int contractMonths)
32 throws Exception {
33 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
34 // 将字符串形式的日期转化为Date格式的日期
35 Date d = sdf.parse(str);
36 Date date = expDate(d, contractMonths);
37 String exp = sdf.format(date);
38 return exp;
39 }
40
41 @SuppressWarnings("unused")
42 public static Date expDate(Date entryDay, int months) {
43 Calendar cal = new GregorianCalendar();
44 cal.setTime(entryDay);
45 cal.add(Calendar.MONTH, months);
46 cal.add(Calendar.MONTH, -1);
47 //测试 合同到期前一个月的日期为一个星期的第几天
48 System.out.println(cal.get(Calendar.DAY_OF_WEEK));
49 int dayOfWeek=cal.get(Calendar.DAY_OF_WEEK);
50 //测试 星期天为第几天
51 System.out.println(Calendar.SUNDAY);
52 // if (dayOfWeek== Calendar.SUNDAY || dayOfWeek == Calendar.SATURDAY) {
53 // cal.add(Calendar.WEEK_OF_MONTH, 1);
54 // cal.set(Calendar.DAY_OF_WEEK, 0);
55 // date = cal.getTime();
56 // } else
57 // date = cal.getTime();
58 int plus = 0;
59 if(dayOfWeek == Calendar.SUNDAY){
60 plus = 1;
61 }else if(dayOfWeek == Calendar.SATURDAY){
62 plus = 2;
63 }
64 cal.add(Calendar.DATE, plus);
65 Date date=cal.getTime();
66 return date;
67 }


相关阅读:
Top