/** * Returns true iff this date contains the date represented by other. A range contains a date if * it is equal to or after the start date and equal to or before the end date. For open ranges, * contains is also inclusive of the one end point. */ public boolean contains(ISODateInstance other) { if (this.isUnparseable() || other.isUnparseable()) { return this.isoDate.equals(other.isoDate); } String start = this.getStartDate(); if (!start.equals("")) { // we have a start date, need to make sure other is after it String startOther = other.getStartDate(); if (startOther.equals("")) { return false; // incompatible } else { if (!isAfter(startOther, start)) { return false; } } } // now we've found out that the start date is appropriate, check the end date String end = this.getEndDate(); if (!end.equals("")) { String endOther = other.getEndDate(); if (endOther.equals("")) { return false; } else { if (!isAfter(end, endOther)) { return false; } } } return true; // passes both start and end }
private void incrementMonth(ISODateInstance referenceDate, Pair<DateField, Integer> relation) { String origDateString = referenceDate.getStartDate(); String monthString = origDateString.substring(4, 6); if (monthString.contains("*")) { isoDate = origDateString; return; } // Month is not a variable Integer monthNum = Integer.parseInt(monthString); // Check if we're an edge case if (((monthNum + relation.second()) > 12) || ((monthNum + relation.second) < 1)) { boolean decreasing = ((monthNum + relation.second) < 1); int newMonthNum = (monthNum + relation.second()) % 12; if (newMonthNum < 0) { newMonthNum *= -1; } // Set the month appropriately isoDate = makeStringMonthChange(origDateString, newMonthNum); // Increment the year if possible String yearString = origDateString.substring(0, 4); if (!yearString.contains("*")) { // How much we increment depends on above mod int numYearsToIncrement = (int) Math.ceil(relation.second() / 12.0); if (decreasing) { isoDate = makeStringYearChange(isoDate, Integer.parseInt(yearString) - numYearsToIncrement); } else { isoDate = makeStringYearChange(isoDate, Integer.parseInt(yearString) + numYearsToIncrement); } } } else { isoDate = makeStringMonthChange(origDateString, (monthNum + relation.second())); } }
private void incrementYear(ISODateInstance referenceDate, Pair<DateField, Integer> relation) { String origDateString = referenceDate.getStartDate(); String yearString = origDateString.substring(0, 4); if (yearString.contains("*")) { isoDate = origDateString; return; } isoDate = makeStringYearChange(origDateString, Integer.parseInt(yearString) + relation.second()); }
/** Constructor for a range of dates, beginning at date start and finishing at date end */ public ISODateInstance(ISODateInstance start, ISODateInstance end) { String startString = start.getDateString(); if (start.isRange()) { startString = start.getStartDate(); } String endString = end.getDateString(); if (end.isRange()) { endString = end.getEndDate(); } isoDate = startString + '/' + endString; unparseable = (start.isUnparseable() || end.isUnparseable()); }
private void incrementDay(ISODateInstance referenceDate, Pair<DateField, Integer> relation) { String origDateString = referenceDate.getStartDate(); String dayString = origDateString.substring(origDateString.length() - 2, origDateString.length()); if (dayString.contains("*")) { isoDate = origDateString; return; } // Date is not a variable Integer dayNum = Integer.parseInt(dayString); String monthString = origDateString.substring(origDateString.length() - 4, origDateString.length() - 2); int numDaysInMonth = 30; // default - assume this if month is a variable int monthNum = -1; // ie, we don't know the month yet - this remains -1 if the month is a variable if (!monthString.contains("*")) { // Set appropriate numDaysInMonth and monthNum monthNum = Integer.parseInt(monthString); numDaysInMonth = daysPerMonth.get(monthNum); } // Now, find out if we're an edge case (potential to increment month) if (dayNum + relation.second() <= numDaysInMonth && dayNum + relation.second() >= 1) { // Not an edge case - just increment the day, create a new string, and return dayNum += relation.second(); isoDate = makeStringDayChange(origDateString, dayNum); return; } // Since we're an edge case, the month can't be a variable - if it is a variable, just set this // to the reference string if (monthNum == -1) { isoDate = origDateString; return; } // At this point, neither our day nor our month is a variable isoDate = origDateString; boolean decreasing = (dayNum + relation.second() < 1); // Need to increment the month, set the date appropriately - we need the new month num to set // the day appropriately, so do month first int newMonthNum; // Now, check if we're an edge case for month if ((monthNum + 1 > 12 && !decreasing) || (monthNum - 1 < 1 && decreasing)) { // First, change the month if (decreasing) { newMonthNum = 12; } else { newMonthNum = 1; } // If we can, increment the year // TODO: fix this to work more nicely with variables and thus handle more cases String yearString = origDateString.substring(0, 4); if (!yearString.contains("*")) { if (decreasing) { isoDate = makeStringYearChange(isoDate, Integer.parseInt(yearString) - 1); } else { isoDate = makeStringYearChange(isoDate, Integer.parseInt(yearString) + 1); } } } else { // We're not an edge case for month - just increment if (decreasing) { newMonthNum = monthNum - 1; } else { newMonthNum = monthNum + 1; } } // do the increment isoDate = makeStringMonthChange(isoDate, newMonthNum); int newDateNum; if (decreasing) { newDateNum = -relation.second() + daysPerMonth.get(newMonthNum) - dayNum; } else { newDateNum = relation.second() - dayNum + daysPerMonth.get(monthNum); } // Now, change the day in our original string to be appropriate isoDate = makeStringDayChange(isoDate, newDateNum); }