Search in sources :

Example 11 with DateTimeField

use of org.joda.time.DateTimeField in project joda-time by JodaOrg.

the class AbstractReadableInstantFieldProperty method toInterval.

/**
     * Returns the interval that represents the range of the minimum
     * and maximum values of this field.
     * <p>
     * For example, <code>datetime.monthOfYear().toInterval()</code>
     * will return an interval over the whole month.
     *
     * @return the interval of this field
     * @since 1.2
     */
public Interval toInterval() {
    DateTimeField field = getField();
    long start = field.roundFloor(getMillis());
    long end = field.add(start, 1);
    Interval interval = new Interval(start, end, getChronology());
    return interval;
}
Also used : DateTimeField(org.joda.time.DateTimeField) Interval(org.joda.time.Interval)

Example 12 with DateTimeField

use of org.joda.time.DateTimeField in project joda-time by JodaOrg.

the class BaseDateTimeField method add.

/**
     * Adds a value (which may be negative) to the partial instant,
     * throwing an exception if the maximum size of the instant is reached.
     * <p>
     * The value will be added to this field, overflowing into larger fields
     * if necessary. Smaller fields should be unaffected, except where the
     * result would be an invalid value for a smaller field. In this case the
     * smaller field is adjusted to be in range.
     * <p>
     * Partial instants only contain some fields. This may result in a maximum
     * possible value, such as TimeOfDay being limited to 23:59:59:999. If this
     * limit is breached by the add an exception is thrown.
     * <p>
     * For example, in the ISO chronology:<br>
     * 2000-08-20 add six months is 2000-02-20<br>
     * 2000-08-20 add twenty months is 2000-04-20<br>
     * 2000-08-20 add minus nine months is 2000-11-20<br>
     * 2001-01-31 add one month  is 2001-02-28<br>
     * 2001-01-31 add two months is 2001-03-31<br>
     * 
     * @param instant  the partial instant
     * @param fieldIndex  the index of this field in the partial
     * @param values  the values of the partial instant which should be updated
     * @param valueToAdd  the value to add, in the units of the field
     * @return the passed in values
     * @throws IllegalArgumentException if the value is invalid or the maximum instant is reached
     */
public int[] add(ReadablePartial instant, int fieldIndex, int[] values, int valueToAdd) {
    if (valueToAdd == 0) {
        return values;
    }
    // there are more efficient algorithms than this (especially for time only fields)
    // trouble is when dealing with days and months, so we use this technique of
    // adding/removing one from the larger field at a time
    DateTimeField nextField = null;
    while (valueToAdd > 0) {
        int max = getMaximumValue(instant, values);
        long proposed = values[fieldIndex] + valueToAdd;
        if (proposed <= max) {
            values[fieldIndex] = (int) proposed;
            break;
        }
        if (nextField == null) {
            if (fieldIndex == 0) {
                throw new IllegalArgumentException("Maximum value exceeded for add");
            }
            nextField = instant.getField(fieldIndex - 1);
            // test only works if this field is UTC (ie. local)
            if (getRangeDurationField().getType() != nextField.getDurationField().getType()) {
                throw new IllegalArgumentException("Fields invalid for add");
            }
        }
        // reduce the amount to add
        valueToAdd -= (max + 1) - values[fieldIndex];
        // add 1 to next bigger field
        values = nextField.add(instant, fieldIndex - 1, values, 1);
        // reset this field to zero
        values[fieldIndex] = getMinimumValue(instant, values);
    }
    while (valueToAdd < 0) {
        int min = getMinimumValue(instant, values);
        long proposed = values[fieldIndex] + valueToAdd;
        if (proposed >= min) {
            values[fieldIndex] = (int) proposed;
            break;
        }
        if (nextField == null) {
            if (fieldIndex == 0) {
                throw new IllegalArgumentException("Maximum value exceeded for add");
            }
            nextField = instant.getField(fieldIndex - 1);
            if (getRangeDurationField().getType() != nextField.getDurationField().getType()) {
                throw new IllegalArgumentException("Fields invalid for add");
            }
        }
        // reduce the amount to add
        valueToAdd -= (min - 1) - values[fieldIndex];
        // subtract 1 from next bigger field
        values = nextField.add(instant, fieldIndex - 1, values, -1);
        // reset this field to max value
        values[fieldIndex] = getMaximumValue(instant, values);
    }
    // adjusts smaller fields
    return set(instant, fieldIndex, values, values[fieldIndex]);
}
Also used : DateTimeField(org.joda.time.DateTimeField)

Example 13 with DateTimeField

use of org.joda.time.DateTimeField in project joda-time by JodaOrg.

the class BasePartial method setValue.

//-----------------------------------------------------------------------
/**
     * Sets the value of the field at the specified index.
     * <p>
     * In version 2.0 and later, this method copies the array into the original.
     * This is because the instance variable has been changed to be final to satisfy the Java Memory Model.
     * This only impacts subclasses that are mutable.
     * 
     * @param index  the index
     * @param value  the value to set
     * @throws IndexOutOfBoundsException if the index is invalid
     */
protected void setValue(int index, int value) {
    DateTimeField field = getField(index);
    int[] values = field.set(this, index, iValues, value);
    System.arraycopy(values, 0, iValues, 0, iValues.length);
}
Also used : DateTimeField(org.joda.time.DateTimeField)

Example 14 with DateTimeField

use of org.joda.time.DateTimeField in project joda-time by JodaOrg.

the class BaseChronology method validate.

//-----------------------------------------------------------------------
/**
     * 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 unless the partial is empty
     * @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(), Integer.valueOf(value), Integer.valueOf(field.getMinimumValue()), null);
        }
        if (value > field.getMaximumValue()) {
            throw new IllegalFieldValueException(field.getType(), Integer.valueOf(value), null, Integer.valueOf(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(), Integer.valueOf(value), Integer.valueOf(field.getMinimumValue(partial, values)), null);
        }
        if (value > field.getMaximumValue(partial, values)) {
            throw new IllegalFieldValueException(field.getType(), Integer.valueOf(value), null, Integer.valueOf(field.getMaximumValue(partial, values)));
        }
    }
}
Also used : IllegalFieldValueException(org.joda.time.IllegalFieldValueException) DateTimeField(org.joda.time.DateTimeField) UnsupportedDateTimeField(org.joda.time.field.UnsupportedDateTimeField)

Example 15 with DateTimeField

use of org.joda.time.DateTimeField in project joda-time by JodaOrg.

the class BasicChronology method assemble.

protected void assemble(Fields fields) {
    // First copy fields that are the same for all Gregorian and Julian
    // chronologies.
    fields.millis = cMillisField;
    fields.seconds = cSecondsField;
    fields.minutes = cMinutesField;
    fields.hours = cHoursField;
    fields.halfdays = cHalfdaysField;
    fields.days = cDaysField;
    fields.weeks = cWeeksField;
    fields.millisOfSecond = cMillisOfSecondField;
    fields.millisOfDay = cMillisOfDayField;
    fields.secondOfMinute = cSecondOfMinuteField;
    fields.secondOfDay = cSecondOfDayField;
    fields.minuteOfHour = cMinuteOfHourField;
    fields.minuteOfDay = cMinuteOfDayField;
    fields.hourOfDay = cHourOfDayField;
    fields.hourOfHalfday = cHourOfHalfdayField;
    fields.clockhourOfDay = cClockhourOfDayField;
    fields.clockhourOfHalfday = cClockhourOfHalfdayField;
    fields.halfdayOfDay = cHalfdayOfDayField;
    // Now create fields that have unique behavior for Gregorian and Julian
    // chronologies.
    fields.year = new BasicYearDateTimeField(this);
    fields.yearOfEra = new GJYearOfEraDateTimeField(fields.year, this);
    // Define one-based centuryOfEra and yearOfCentury.
    DateTimeField field = new OffsetDateTimeField(fields.yearOfEra, 99);
    fields.centuryOfEra = new DividedDateTimeField(field, DateTimeFieldType.centuryOfEra(), 100);
    fields.centuries = fields.centuryOfEra.getDurationField();
    field = new RemainderDateTimeField((DividedDateTimeField) fields.centuryOfEra);
    fields.yearOfCentury = new OffsetDateTimeField(field, DateTimeFieldType.yearOfCentury(), 1);
    fields.era = new GJEraDateTimeField(this);
    fields.dayOfWeek = new GJDayOfWeekDateTimeField(this, fields.days);
    fields.dayOfMonth = new BasicDayOfMonthDateTimeField(this, fields.days);
    fields.dayOfYear = new BasicDayOfYearDateTimeField(this, fields.days);
    fields.monthOfYear = new GJMonthOfYearDateTimeField(this);
    fields.weekyear = new BasicWeekyearDateTimeField(this);
    fields.weekOfWeekyear = new BasicWeekOfWeekyearDateTimeField(this, fields.weeks);
    field = new RemainderDateTimeField(fields.weekyear, fields.centuries, DateTimeFieldType.weekyearOfCentury(), 100);
    fields.weekyearOfCentury = new OffsetDateTimeField(field, DateTimeFieldType.weekyearOfCentury(), 1);
    // The remaining (imprecise) durations are available from the newly
    // created datetime fields.
    fields.years = fields.year.getDurationField();
    fields.months = fields.monthOfYear.getDurationField();
    fields.weekyears = fields.weekyear.getDurationField();
}
Also used : OffsetDateTimeField(org.joda.time.field.OffsetDateTimeField) RemainderDateTimeField(org.joda.time.field.RemainderDateTimeField) ZeroIsMaxDateTimeField(org.joda.time.field.ZeroIsMaxDateTimeField) PreciseDateTimeField(org.joda.time.field.PreciseDateTimeField) OffsetDateTimeField(org.joda.time.field.OffsetDateTimeField) RemainderDateTimeField(org.joda.time.field.RemainderDateTimeField) DateTimeField(org.joda.time.DateTimeField) DividedDateTimeField(org.joda.time.field.DividedDateTimeField) DividedDateTimeField(org.joda.time.field.DividedDateTimeField)

Aggregations

DateTimeField (org.joda.time.DateTimeField)20 DateTime (org.joda.time.DateTime)5 DividedDateTimeField (org.joda.time.field.DividedDateTimeField)2 OffsetDateTimeField (org.joda.time.field.OffsetDateTimeField)2 RemainderDateTimeField (org.joda.time.field.RemainderDateTimeField)2 DateTimeFieldType (org.joda.time.DateTimeFieldType)1 IllegalFieldValueException (org.joda.time.IllegalFieldValueException)1 Interval (org.joda.time.Interval)1 Period (org.joda.time.Period)1 DelegatedDateTimeField (org.joda.time.field.DelegatedDateTimeField)1 PreciseDateTimeField (org.joda.time.field.PreciseDateTimeField)1 SkipUndoDateTimeField (org.joda.time.field.SkipUndoDateTimeField)1 UnsupportedDateTimeField (org.joda.time.field.UnsupportedDateTimeField)1 ZeroIsMaxDateTimeField (org.joda.time.field.ZeroIsMaxDateTimeField)1