@Test public void testGetTime() { assertEquals(10L, new MyDate(10L).getTime()); MyDate date = new MyDate(new Date()); long old = date.toDays(); assertEquals(old + 1, new MyDate(date).plusDays(1).toDays()); assertEquals(old - 1, new MyDate(date).minusDays(1).toDays()); }
// return true if the syntax of the date valid public static Boolean isDateSyntaxValid(String date) { // System.out.println("date length " + date.length()); if (date.length() != GlobalVar.DATE_LEN) { return false; } String num = date.replaceAll("[^\\d]", ""); // remove all the non-digits chars // throw illegal argeument exception if non-digits characters passed in if (num.length() != GlobalVar.DATE_LEN) { return false; } MyDate myDate = new MyDate(date); // if string passed in can't create a new date, get null if (myDate.getDay() == null) { return false; } else { return true; } }
public static String computeNumOfDays(MyDate so, MyDate si) { if (so != null && si != null) { String days = "" + so.getDaysDiff(si); while (days.length() < GlobalVar.ORDER_LEAVE_DAYS_IN_DMO) { days = "0" + days; } return days; } else { return null; } }
public static boolean isDateTime(String dateTime) { String[] strArr = dateTime.split("-|\\s|:"); if (strArr.length < 6) return false; int year = Integer.parseInt(strArr[0]); int month = Integer.parseInt(strArr[1]); int day = Integer.parseInt(strArr[2]); if (!MyDate.isDate(year + "-" + month + "-" + day)) return false; int hour = Integer.parseInt(strArr[3]); if (hour < 0 || hour > 23) return false; int minute = Integer.parseInt(strArr[4]); if (minute < 0 || minute > 59) return false; int second = Integer.parseInt(strArr[5]); if (second < 0 || second > 59) return false; return true; }
// check if the leaves have been previously logged. // return GOOD_LEAVE if passed in leave is a valid one // return DUPLICATE_CTRL_NUM if it has duplicate ctrl number // return OVERLAP_LEAVE if its overlaps with previouos leaves. // Map<String, Map<String, List<String>>> has key of SSN, dates of values, leaves posted before public static int checkLeaves( String ctrl, String SSN, String SO, String SI, String first5, Map<String, Map<String, List<String>>> leaves) { if (ctrl != null && SSN != null && SO != null && SI != null && first5 != null) { MyDate today = new MyDate(); MyDate newSO = new MyDate(SO); MyDate newSI = new MyDate(SI); if (newSO.after(newSI)) { return SIGN_IN_SIGN_OUT_ERR; } if (newSO.after(today) || newSO.after(today)) { return FUTURE_SIGN_IN_OUT_ERR; } if (!leaves.containsKey(SSN)) { Map<String, List<String>> value = new HashMap<String, List<String>>(); List<String> dates = new ArrayList<String>(); dates.add(SO); dates.add(SI); value.put(ctrl, dates); leaves.put(SSN, value); return GOOD_LEAVE; } else { // has the ssn Map<String, List<String>> value = leaves.get(SSN); // Map<ctrlNum, List<leave>> if (value.containsKey(ctrl)) { return DUPLICATE_CTRL_NUM_ERR; // duplicate control number } else { // look through each leave Set<String> valueSet = new HashSet<>(value.keySet()); for (String thisCtrl : valueSet) { // System.out.println(thisCtrl); List<String> dates = value.get(thisCtrl); MyDate thisSO = new MyDate(dates.get(0)); MyDate thisSI = new MyDate(dates.get(1)); // System.out.println(); if (newSO.getDaysDiff(today) > ELEVEN_MONTH) { return IAS_ERR; } if (newSO.beforeOrEqual(thisSI) && newSI.afterOrEqual(thisSO)) { System.out.println("GlobalVar.java: overlap leaves 1."); return OVERLAP_LEAVE_ERR; } else if (newSI.afterOrEqual(thisSO) && newSI.beforeOrEqual(thisSI)) { // System.out.println("GlobalVar.java: overlap leaves 2. "); return OVERLAP_LEAVE_ERR; } else if (newSI.afterOrEqual(thisSI) && newSO.beforeOrEqual(thisSO)) { System.out.println("GlobalVar.java: overlap leaves 3."); return OVERLAP_LEAVE_ERR; } else { // ok to add List<String> newDates = new ArrayList<String>(); newDates.add(SO); newDates.add(SI); value.put(ctrl, newDates); // return GOOD_LEAVE; } } // if come here, its a good leave return GOOD_LEAVE; } } } else if (SSN != null && (SI == null || SO == null)) { return NULL_DATE_ERR; } else if (SSN != null) { return NULL_SSN_ERR; } else { return NULL_INFO_ERR; } }
/** * Allocates a <code>MyDate</code> object and initializes it to represent the day (tomorrow) which * follows the provided day, month and year. * * @param day the day. * @param month the month. * @param year the year. * @return a (my)date object initialized to next day of (day, month, year) */ public static MyDate nextDay(int day, int month, int year) { MyDate date = new MyDate(day, month, year); return date.nextDay(); }