/** * Validates whether the fields stored in a partial instant are valid. * * <p>This implementation uses {@link DateTimeField#getMinimumValue(ReadablePartial, int[])} and * {@link DateTimeField#getMaximumValue(ReadablePartial, int[])}. * * @param partial the partial instant to validate * @param values the values to validate, not null * @throws IllegalArgumentException if the instant is invalid */ public void validate(ReadablePartial partial, int[] values) { // check values in standard range, catching really stupid cases like -1 // this means that the second check will not hit trouble int size = partial.size(); for (int i = 0; i < size; i++) { int value = values[i]; DateTimeField field = partial.getField(i); if (value < field.getMinimumValue()) { throw new IllegalFieldValueException( field.getType(), new Integer(value), new Integer(field.getMinimumValue()), null); } if (value > field.getMaximumValue()) { throw new IllegalFieldValueException( field.getType(), new Integer(value), null, new Integer(field.getMaximumValue())); } } // check values in specific range, catching really odd cases like 30th Feb for (int i = 0; i < size; i++) { int value = values[i]; DateTimeField field = partial.getField(i); if (value < field.getMinimumValue(partial, values)) { throw new IllegalFieldValueException( field.getType(), new Integer(value), new Integer(field.getMinimumValue(partial, values)), null); } if (value > field.getMaximumValue(partial, values)) { throw new IllegalFieldValueException( field.getType(), new Integer(value), null, new Integer(field.getMaximumValue(partial, values))); } } }
/** * Given a year, month, and day, find the number of occurrences of that day in the month * * @param year the year * @param month the month * @param day the day * @return the number of occurrences of the day in the month */ public int numOccurrences(int year, int month, int day) { DateTimeFormatter parser = ISODateTimeFormat.date(); DateTime date = parser.parseDateTime(year + "-" + month + "-" + "01"); Calendar cal = Calendar.getInstance(); cal.setTime(date.toDate()); GregorianChronology calendar = GregorianChronology.getInstance(); DateTimeField field = calendar.dayOfMonth(); int days = 0; int count = 0; int num = field.getMaximumValue(new LocalDate(year, month, day, calendar)); while (days < num) { if (cal.get(Calendar.DAY_OF_WEEK) == day) { count++; } date = date.plusDays(1); cal.setTime(date.toDate()); days++; } return count; }
public int getMaximumValue(ReadablePartial instant, int[] values) { return iField.getMaximumValue(instant, values); }
public int getMaximumValue(ReadablePartial instant) { return iField.getMaximumValue(instant); }
public int getMaximumValue(long instant) { long localInstant = iZone.convertUTCToLocal(instant); return iField.getMaximumValue(localInstant); }
public int getMaximumValue() { return iField.getMaximumValue(); }
/** Tests the dayOfMonth DateTimeField */ @Test public void testDayOfMonthField() { DateTimeField dayOfMonthField = CHRON_NOLEAP.dayOfMonth(); assertEquals(1, dayOfMonthField.getMinimumValue()); assertEquals(31, dayOfMonthField.getMaximumValue()); }