ENDOFMONTH TAG in Automation Framework

Add a new tag <ENDOFMONTH> in Automation Framework.

 1 /**
 2   * @param input
 3   *            <ENDOFMONTH+1M><ENDOFMONTH-1Y>
 4   * @return Date "dd/MM/yyyy"
 5   */
 6  public String EndofMonth(String input) {
 7   SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
 8   Date today = new Date();
 9   Calendar calendar = Calendar.getInstance();
10   input = input.substring(1, input.length() - 1);
11   int year = calendar.get(calendar.YEAR);
12   int month = calendar.get(calendar.MONTH);
13   if (input.length() == 10) {
14    int maxDay = calendar.getActualMaximum(calendar.DAY_OF_MONTH);
15    calendar.set(year, month, maxDay);
16    return dateFormat.format(calendar.getTime());
17   } else if (input.length() > 10) {
18    String math = input.substring(10, 11);
19    String scale = input.substring(input.length() - 1);
20    int num = Integer.parseInt(input.substring(input.indexOf(math) + 1,
21      input.length() - 1));
22    if (math.equals("-")) {
23     num = -num;
24    } else if (!math.equals("+")) {
25     return "Operator error!";
26    }
27    if (scale.equalsIgnoreCase("Y")) {
28     year = year + num;
29     int maxDay = calendar.getActualMaximum(calendar.DAY_OF_MONTH);
30     calendar.set(calendar.YEAR, year);
31     calendar.set(calendar.get(calendar.YEAR), month, maxDay);
32     return dateFormat.format(calendar.getTime());
33    } else if (scale.equalsIgnoreCase("M")) {
34     month = month + num;
35     if (month >= 12) {
36      int yearIncrease = month / 12;
37      int monthIncrease = month % 12;
38      year = year + yearIncrease;
39      month = monthIncrease;
40      calendar.set(calendar.YEAR, year);
41      calendar.set(calendar.MONTH, month);
42      int maxDay = calendar
43        .getActualMaximum(calendar.DAY_OF_MONTH);
44      calendar.set(calendar.get(calendar.YEAR),
45        calendar.get(calendar.MONTH), maxDay);
46      return dateFormat.format(calendar.getTime());
47     } else if (month < 1) {
48      int tempMonth = Math.abs(month);
49      int yearDecrease = tempMonth / 12;
50      int monthDecrease = tempMonth % 12;
51      year = year - yearDecrease;
52      month = calendar.get(calendar.MONTH) - monthDecrease;
53      calendar.set(calendar.YEAR, year);
54      calendar.set(calendar.MONTH, month);
55      int maxDay = calendar
56        .getActualMaximum(calendar.DAY_OF_MONTH);
57      calendar.set(calendar.get(calendar.YEAR),
58        calendar.get(calendar.MONTH), maxDay);
59      return dateFormat.format(calendar.getTime());
60     }else{
61     calendar.set(calendar.MONTH, month);
62      int maxDay = calendar
63        .getActualMaximum(calendar.DAY_OF_MONTH);
64      calendar.set(calendar.get(calendar.YEAR),
65        calendar.get(calendar.MONTH), maxDay);
66      return dateFormat.format(calendar.getTime());
67     }
68    }
69   } else {
70    return "The format of input value " + input + " is incorrect.";
71   }
72   return "No information available!";
73  }

 

posted @ 2013-01-22 00:12  平静缓和用胸音说爱  阅读(211)  评论(0编辑  收藏  举报