public static DateTime nextApplicableWeekDayIncludingFromDate(DateTime fromDate, List<DayOfWeek> applicableDays) { if (CollectionUtils.isEmpty(applicableDays)) { throw new IllegalArgumentException("Applicable Days should not be empty"); } int dayOfWeek = fromDate.getDayOfWeek(); int noOfDaysToNearestCycleDate = 0; int weekMaxDay = DayOfWeek.Sunday.getValue(); int currentDayOfWeek = dayOfWeek; for (int dayCount = 0; dayCount <= weekMaxDay; dayCount++) { if (applicableDays.contains(getDayOfWeek(currentDayOfWeek))) { noOfDaysToNearestCycleDate = dayCount; break; } if (currentDayOfWeek == weekMaxDay) { currentDayOfWeek = 1; } else { currentDayOfWeek++; } } return fromDate.dayOfMonth().addToCopy(noOfDaysToNearestCycleDate); }
/** * Determine the ending DateTime (exclusive) of an interval based on an instant in time * * @param instant The start of an instant * @return */ public DateTime determineEnd(DateTime instant) { if (this.dateTimeFieldType != null) { final DateTime start = instant.property(this.dateTimeFieldType).roundFloorCopy(); return start.property(this.dateTimeFieldType).addToCopy(1); } if (this == AggregationInterval.FIVE_MINUTE) { final DateTime start = instant.hourOfDay() .roundFloorCopy() .plusMinutes((instant.getMinuteOfHour() / 5) * 5); return start.plusMinutes(5); } throw new IllegalArgumentException( "Cannot compute interval end time for " + this + " please use " + AggregationIntervalHelper.class); } }
public static DateTime nextApplicableWeekDay(DateTime fromDate, List<DayOfWeek> applicableDays) { return nextApplicableWeekDayIncludingFromDate(fromDate.dayOfMonth().addToCopy(1), applicableDays); }
/** * Round the date time back to the beginning of the nearest (inclusive) month of January, April, July, October. * * @param from the date being rounded * * @return The nearest previous start of month for one of the three quarter months */ public static DateTime quarterlyRound(DateTime from) { DateTime.Property property = from.monthOfYear(); // Shift the month from a one to a zero basis (Jan == 0), then adjust backwards to one of the months that are // an integer multiple of three months from the start of the year, then round to the start of that month. return property.addToCopy(-1 * ((property.get() - 1) % 3)).monthOfYear().roundFloorCopy(); }