org.joda.time.DurationField Java Examples

The following examples show how to use org.joda.time.DurationField. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: BaseSingleFieldPeriod.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Creates a new instance representing the number of complete standard length units
 * in the specified period.
 * <p>
 * This factory method converts all fields from the period to hours using standardised
 * durations for each field. Only those fields which have a precise duration in
 * the ISO UTC chronology can be converted.
 * <ul>
 * <li>One week consists of 7 days.
 * <li>One day consists of 24 hours.
 * <li>One hour consists of 60 minutes.
 * <li>One minute consists of 60 seconds.
 * <li>One second consists of 1000 milliseconds.
 * </ul>
 * Months and Years are imprecise and periods containing these values cannot be converted.
 *
 * @param period  the period to get the number of hours from, must not be null
 * @param millisPerUnit  the number of milliseconds in one standard unit of this period
 * @throws IllegalArgumentException if the period contains imprecise duration values
 */
protected static int standardPeriodIn(ReadablePeriod period, long millisPerUnit) {
    if (period == null) {
        return 0;
    }
    Chronology iso = ISOChronology.getInstanceUTC();
    long duration = 0L;
    for (int i = 0; i < period.size(); i++) {
        int value = period.getValue(i);
        if (value != 0) {
            DurationField field = period.getFieldType(i).getField(iso);
            if (field.isPrecise() == false) {
                throw new IllegalArgumentException(
                        "Cannot convert period to duration as " + field.getName() +
                        " is not precise in the period " + period);
            }
            duration = FieldUtils.safeAdd(duration, FieldUtils.safeMultiply(field.getUnitMillis(), value));
        }
    }
    return FieldUtils.safeToInt(duration / millisPerUnit);
}
 
Example #2
Source File: DividedDateTimeField.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Constructor.
 * 
 * @param field  the field to wrap, like "year()".
 * @param rangeField  the range field, null to derive
 * @param type  the field type this field will actually use
 * @param divisor  divisor, such as 100 years in a century
 * @throws IllegalArgumentException if divisor is less than two
 */
public DividedDateTimeField(DateTimeField field, DurationField rangeField,
                            DateTimeFieldType type, int divisor) {
    super(field, type);
    if (divisor < 2) {
        throw new IllegalArgumentException("The divisor must be at least 2");
    }
    DurationField unitField = field.getDurationField();
    if (unitField == null) {
        iDurationField = null;
    } else {
        iDurationField = new ScaledDurationField(
            unitField, type.getDurationType(), divisor);
    }
    iRangeDurationField = rangeField;
    iDivisor = divisor;
    int i = field.getMinimumValue();
    int min = (i >= 0) ? i / divisor : ((i + 1) / divisor - 1);
    int j = field.getMaximumValue();
    int max = (j >= 0) ? j / divisor : ((j + 1) / divisor - 1);
    iMin = min;
    iMax = max;
}
 
Example #3
Source File: GJChronology.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Uses a shared duration field rather than creating a new one.
 *
 * @param durationField shared duration field
 */
ImpreciseCutoverField(DateTimeField julianField, DateTimeField gregorianField,
                      DurationField durationField,
                      long cutoverMillis, boolean convertByWeekyear)
{
    super(julianField, gregorianField, cutoverMillis, convertByWeekyear);
    if (durationField == null) {
        durationField = new LinkedDurationField(iDurationField, this);
    }
    iDurationField = durationField;
}
 
Example #4
Source File: BaseChronology.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Gets the values of a period from an interval.
 *
 * @param period  the period instant to use
 * @param startInstant  the start instant of an interval to query
 * @param endInstant  the start instant of an interval to query
 * @return the values of the period extracted from the interval
 */
public int[] get(ReadablePeriod period, long startInstant, long endInstant) {
    int size = period.size();
    int[] values = new int[size];
    if (startInstant != endInstant) {
        for (int i = 0; i < size; i++) {
            DurationField field = period.getFieldType(i).getField(this);
            int value = field.getDifference(endInstant, startInstant);
            startInstant = field.add(startInstant, value);
            values[i] = value;
        }
    }
    return values;
}
 
Example #5
Source File: GJChronology.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Uses a shared duration field rather than creating a new one.
 *
 * @param durationField shared duration field
 */
ImpreciseCutoverField(DateTimeField julianField, DateTimeField gregorianField,
                      DurationField durationField,
                      long cutoverMillis, boolean convertByWeekyear)
{
    super(julianField, gregorianField, cutoverMillis, convertByWeekyear);
    if (durationField == null) {
        durationField = new LinkedDurationField(iDurationField, this);
    }
    iDurationField = durationField;
}
 
Example #6
Source File: Time_24_DateTimeParserBucket_s.java    From coming with MIT License 5 votes vote down vote up
static int compareReverse(DurationField a, DurationField b) {
    if (a == null || !a.isSupported()) {
        if (b == null || !b.isSupported()) {
            return 0;
        }
        return -1;
    }
    if (b == null || !b.isSupported()) {
        return 1;
    }
    return -a.compareTo(b);
}
 
Example #7
Source File: BaseChronology.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Gets the values of a period from an interval.
 *
 * @param period  the period instant to use
 * @param startInstant  the start instant of an interval to query
 * @param endInstant  the start instant of an interval to query
 * @return the values of the period extracted from the interval
 */
public int[] get(ReadablePeriod period, long startInstant, long endInstant) {
    int size = period.size();
    int[] values = new int[size];
    if (startInstant != endInstant) {
        for (int i = 0; i < size; i++) {
            DurationField field = period.getFieldType(i).getField(this);
            int value = field.getDifference(endInstant, startInstant);
            startInstant = field.add(startInstant, value);
            values[i] = value;
        }
    }
    return values;
}
 
Example #8
Source File: GJChronology.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Uses shared duration fields rather than creating a new one.
 *
 * @param durationField shared duration field
 */
ImpreciseCutoverField(DateTimeField julianField, DateTimeField gregorianField,
                      DurationField durationField, DurationField rangeDurationField, long cutoverMillis)
{
    this(julianField, gregorianField, durationField, cutoverMillis, false);
    iRangeDurationField = rangeDurationField;
}
 
Example #9
Source File: DividedDateTimeField.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Constructor.
 * 
 * @param field  the field to wrap, like "year()".
 * @param type  the field type this field will actually use
 * @param divisor  divisor, such as 100 years in a century
 * @throws IllegalArgumentException if divisor is less than two
 */
public DividedDateTimeField(DateTimeField field,
                            DateTimeFieldType type, int divisor) {
    super(field, type);
            
    if (divisor < 2) {
        throw new IllegalArgumentException("The divisor must be at least 2");
    }

    DurationField unitField = field.getDurationField();
    if (unitField == null) {
        iDurationField = null;
    } else {
        iDurationField = new ScaledDurationField(
            unitField, type.getDurationType(), divisor);
    }

    iDivisor = divisor;

    int i = field.getMinimumValue();
    int min = (i >= 0) ? i / divisor : ((i + 1) / divisor - 1);

    int j = field.getMaximumValue();
    int max = (j >= 0) ? j / divisor : ((j + 1) / divisor - 1);

    iMin = min;
    iMax = max;
}
 
Example #10
Source File: ZonedChronology.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
private DurationField convertField(DurationField field, HashMap<Object, Object> converted) {
    if (field == null || !field.isSupported()) {
        return field;
    }
    if (converted.containsKey(field)) {
        return (DurationField)converted.get(field);
    }
    ZonedDurationField zonedField = new ZonedDurationField(field, getZone());
    converted.put(field, zonedField);
    return zonedField;
}
 
Example #11
Source File: PreciseDurationDateTimeField.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Constructor.
 * 
 * @param type  the field type
 * @param unit  precise unit duration, like "days()".
 * @throws IllegalArgumentException if duration field is imprecise
 * @throws IllegalArgumentException if unit milliseconds is less than one
 */
public PreciseDurationDateTimeField(DateTimeFieldType type, DurationField unit) {
    super(type);

    if (!unit.isPrecise()) {
        throw new IllegalArgumentException("Unit duration field must be precise");
    }

    iUnitMillis = unit.getUnitMillis();
    if (iUnitMillis < 1) {
        throw new IllegalArgumentException("The unit milliseconds must be at least 1");
    }

    iUnitField = unit;
}
 
Example #12
Source File: DateTimeParserBucket.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
static int compareReverse(DurationField a, DurationField b) {
    if (a == null || !a.isSupported()) {
        if (b == null || !b.isSupported()) {
            return 0;
        }
        return -1;
    }
    if (b == null || !b.isSupported()) {
        return 1;
    }
    return -a.compareTo(b);
}
 
Example #13
Source File: Time_2_UnsupportedDurationField_t.java    From coming with MIT License 5 votes vote down vote up
/**
 * Always returns zero, indicating that sort order is not relevent.
 *
 * @return zero always
 */
public int compareTo(DurationField durationField) {
    if (durationField.isSupported()) {
        return 1;
    }
    return 0;
}
 
Example #14
Source File: JGenProg2017_00142_s.java    From coming with MIT License 4 votes vote down vote up
public DurationField getLeapDurationField() {
    return getWrappedField().getLeapDurationField();
}
 
Example #15
Source File: DelegatedDateTimeField.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
public DurationField getRangeDurationField() {
    if (iRangeDurationField != null) {
        return iRangeDurationField;
    }
    return iField.getRangeDurationField();
}
 
Example #16
Source File: Time_6_GJChronology_t.java    From coming with MIT License 4 votes vote down vote up
LinkedDurationField(DurationField durationField, ImpreciseCutoverField dateTimeField) {
    super(durationField, durationField.getType());
    iField = dateTimeField;
}
 
Example #17
Source File: TestPreciseDurationDateTimeField.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
protected MockPreciseDurationDateTimeField(DateTimeFieldType type, DurationField dur) {
    super(type, dur);
}
 
Example #18
Source File: FixedYearLengthChronology.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
/** Returns null: the field has no range */
@Override
public DurationField getRangeDurationField() {
  return null;
}
 
Example #19
Source File: JGenProg2017_00123_s.java    From coming with MIT License 4 votes vote down vote up
public DurationField getLeapDurationField() {
    return getWrappedField().getLeapDurationField();
}
 
Example #20
Source File: AcademicYearDateTimeField.java    From fenixedu-academic with GNU Lesser General Public License v3.0 4 votes vote down vote up
@Override
public DurationField getRangeDurationField() {

    throw unsupported();
}
 
Example #21
Source File: JGenProg2017_00123_t.java    From coming with MIT License 4 votes vote down vote up
public DurationField getLeapDurationField() {
    return getWrappedField().getLeapDurationField();
}
 
Example #22
Source File: AgeCalculator.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
public long updateResult(long minuend, long subtrahend) {
    // Because time zone can be dynamically changed, field must be
    // dynamically acquired.

    DurationField field;
    switch (iFieldType) {
    case YEARS:
        field = iChronology.years();
        break;
    case MONTHS:
        field = iChronology.months();
        break;
    case DAYS:
        field = iChronology.days();
        break;
    case WEEKYEARS:
        field = iChronology.weekyears();
        break;
    case WEEKS:
        field = iChronology.weeks();
        break;
    case HOURS:
        field = iChronology.hours();
        break;
    case MINUTES:
        field = iChronology.minutes();
        break;
    case SECONDS: default:
        field = iChronology.seconds();
        break;
    }

    String textToSet = "";

    if (iCheckbox.isSelected()) {
        long difference = field.getDifferenceAsLong(minuend, subtrahend);
        textToSet = Long.toString(difference);
        subtrahend = field.add(subtrahend, difference);
    }

    if (!iResult.getText().equals(textToSet)) {
        iResult.setText(textToSet);
    }

    return subtrahend;
}
 
Example #23
Source File: BasicSingleEraDateTimeField.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/** @inheritDoc */
public DurationField getRangeDurationField() {
    return null;
}
 
Example #24
Source File: TestEthiopicChronology.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
public void testDurationMonth() {
    // Leap 1999, NotLeap 1996,97,98
    DateTime dt11 = new DateTime(1999, 11, 2, 0, 0, 0, 0, ETHIOPIC_UTC);
    DateTime dt12 = new DateTime(1999, 12, 2, 0, 0, 0, 0, ETHIOPIC_UTC);
    DateTime dt13 = new DateTime(1999, 13, 2, 0, 0, 0, 0, ETHIOPIC_UTC);
    DateTime dt01 = new DateTime(2000, 1, 2, 0, 0, 0, 0, ETHIOPIC_UTC);
    
    DurationField fld = dt11.monthOfYear().getDurationField();
    assertEquals(ETHIOPIC_UTC.months(), fld);
    assertEquals(1L * 30L * MILLIS_PER_DAY, fld.getMillis(1, dt11.getMillis()));
    assertEquals(2L * 30L * MILLIS_PER_DAY, fld.getMillis(2, dt11.getMillis()));
    assertEquals((2L * 30L + 6L) * MILLIS_PER_DAY, fld.getMillis(3, dt11.getMillis()));
    assertEquals((3L * 30L + 6L) * MILLIS_PER_DAY, fld.getMillis(4, dt11.getMillis()));
    
    assertEquals(1L * 30L * MILLIS_PER_DAY, fld.getMillis(1));
    assertEquals(2L * 30L * MILLIS_PER_DAY, fld.getMillis(2));
    assertEquals(13L * 30L * MILLIS_PER_DAY, fld.getMillis(13));
    
    assertEquals(1L * 30L * MILLIS_PER_DAY, fld.getMillis(1L, dt11.getMillis()));
    assertEquals(2L * 30L * MILLIS_PER_DAY, fld.getMillis(2L, dt11.getMillis()));
    assertEquals((2L * 30L + 6L) * MILLIS_PER_DAY, fld.getMillis(3L, dt11.getMillis()));
    assertEquals((3L * 30L + 6L) * MILLIS_PER_DAY, fld.getMillis(4L, dt11.getMillis()));
    
    assertEquals(1L * 30L * MILLIS_PER_DAY, fld.getMillis(1L));
    assertEquals(2L * 30L * MILLIS_PER_DAY, fld.getMillis(2L));
    assertEquals(13L * 30L * MILLIS_PER_DAY, fld.getMillis(13L));
    
    assertEquals(0, fld.getValue(1L * 30L * MILLIS_PER_DAY - 1L, dt11.getMillis()));
    assertEquals(1, fld.getValue(1L * 30L * MILLIS_PER_DAY, dt11.getMillis()));
    assertEquals(1, fld.getValue(1L * 30L * MILLIS_PER_DAY + 1L, dt11.getMillis()));
    assertEquals(1, fld.getValue(2L * 30L * MILLIS_PER_DAY - 1L, dt11.getMillis()));
    assertEquals(2, fld.getValue(2L * 30L * MILLIS_PER_DAY, dt11.getMillis()));
    assertEquals(2, fld.getValue(2L * 30L * MILLIS_PER_DAY + 1L, dt11.getMillis()));
    assertEquals(2, fld.getValue((2L * 30L + 6L) * MILLIS_PER_DAY - 1L, dt11.getMillis()));
    assertEquals(3, fld.getValue((2L * 30L + 6L) * MILLIS_PER_DAY, dt11.getMillis()));
    assertEquals(3, fld.getValue((2L * 30L + 6L) * MILLIS_PER_DAY + 1L, dt11.getMillis()));
    assertEquals(3, fld.getValue((3L * 30L + 6L) * MILLIS_PER_DAY - 1L, dt11.getMillis()));
    assertEquals(4, fld.getValue((3L * 30L + 6L) * MILLIS_PER_DAY, dt11.getMillis()));
    assertEquals(4, fld.getValue((3L * 30L + 6L) * MILLIS_PER_DAY + 1L, dt11.getMillis()));
    
    assertEquals(dt12.getMillis(), fld.add(dt11.getMillis(), 1));
    assertEquals(dt13.getMillis(), fld.add(dt11.getMillis(), 2));
    assertEquals(dt01.getMillis(), fld.add(dt11.getMillis(), 3));
    
    assertEquals(dt12.getMillis(), fld.add(dt11.getMillis(), 1L));
    assertEquals(dt13.getMillis(), fld.add(dt11.getMillis(), 2L));
    assertEquals(dt01.getMillis(), fld.add(dt11.getMillis(), 3L));
}
 
Example #25
Source File: BasicDayOfMonthDateTimeField.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Restricted constructor.
 */
BasicDayOfMonthDateTimeField(BasicChronology chronology, DurationField days) {
    super(DateTimeFieldType.dayOfMonth(), days);
    iChronology = chronology;
}
 
Example #26
Source File: GJChronology.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
public DurationField getLeapDurationField() {
    return iGregorianField.getLeapDurationField();
}
 
Example #27
Source File: Cardumen_00190_s.java    From coming with MIT License 4 votes vote down vote up
public DurationField getLeapDurationField() {
    return getWrappedField().getLeapDurationField();
}
 
Example #28
Source File: Cardumen_0072_t.java    From coming with MIT License 4 votes vote down vote up
public DurationField getLeapDurationField() {
    return getWrappedField().getLeapDurationField();
}
 
Example #29
Source File: Time_14_BasicMonthOfYearDateTimeField_t.java    From coming with MIT License 4 votes vote down vote up
public DurationField getRangeDurationField() {
    return iChronology.years();
}
 
Example #30
Source File: JGenProg2015_0010_s.java    From coming with MIT License 4 votes vote down vote up
public DurationField getLeapDurationField() {
    return getWrappedField().getLeapDurationField();
}