类org.joda.time.DurationField源码实例Demo

下面列出了怎么用org.joda.time.DurationField的API类实例代码及写法,或者点击链接到github查看源代码。

源代码1 项目: astor   文件: BaseSingleFieldPeriod.java
/**
 * 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);
}
 
源代码2 项目: astor   文件: DividedDateTimeField.java
/**
 * 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;
}
 
源代码3 项目: astor   文件: GJChronology.java
/**
 * 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;
}
 
源代码4 项目: astor   文件: BaseChronology.java
/**
 * 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;
}
 
源代码5 项目: astor   文件: GJChronology.java
/**
 * 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;
}
 
源代码6 项目: coming   文件: Time_24_DateTimeParserBucket_s.java
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);
}
 
源代码7 项目: astor   文件: BaseChronology.java
/**
 * 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;
}
 
源代码8 项目: astor   文件: GJChronology.java
/**
 * 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;
}
 
源代码9 项目: astor   文件: DividedDateTimeField.java
/**
 * 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;
}
 
源代码10 项目: astor   文件: ZonedChronology.java
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;
}
 
源代码11 项目: astor   文件: PreciseDurationDateTimeField.java
/**
 * 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;
}
 
源代码12 项目: astor   文件: DateTimeParserBucket.java
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);
}
 
源代码13 项目: coming   文件: Time_2_UnsupportedDurationField_t.java
/**
 * 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;
}
 
源代码14 项目: astor   文件: GJChronology.java
public DurationField getDurationField() {
    return iDurationField;
}
 
源代码15 项目: coming   文件: Time_6_GJChronology_t.java
/**
 * 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)
{
    this(julianField, gregorianField, durationField, cutoverMillis, false);
}
 
源代码16 项目: netcdf-java   文件: FixedYearLengthChronology.java
/** Returns null: the field has no range */
@Override
public DurationField getRangeDurationField() {
  return null;
}
 
源代码17 项目: astor   文件: GJEraDateTimeField.java
public DurationField getDurationField() {
    return UnsupportedDurationField.getInstance(DurationFieldType.eras());
}
 
源代码18 项目: astor   文件: LimitChronology.java
public final DurationField getRangeDurationField() {
    return iRangeDurationField;
}
 
源代码19 项目: astor   文件: GJChronology.java
LinkedDurationField(DurationField durationField, ImpreciseCutoverField dateTimeField) {
    super(durationField, durationField.getType());
    iField = dateTimeField;
}
 
public DurationField getRangeDurationField() {
    return iChronology.years();
}
 
源代码21 项目: astor   文件: AssembledChronology.java
public final DurationField days() {
    return iDays;
}
 
源代码22 项目: astor   文件: TestGJChronology.java
public DurationField years() {
    return year().getDurationField();
}
 
源代码23 项目: astor   文件: TestCopticChronology.java
public void testDurationMonth() {
    // Leap 1723
    DateTime dt11 = new DateTime(1723, 11, 2, 0, 0, 0, 0, COPTIC_UTC);
    DateTime dt12 = new DateTime(1723, 12, 2, 0, 0, 0, 0, COPTIC_UTC);
    DateTime dt13 = new DateTime(1723, 13, 2, 0, 0, 0, 0, COPTIC_UTC);
    DateTime dt01 = new DateTime(1724, 1, 2, 0, 0, 0, 0, COPTIC_UTC);
    
    DurationField fld = dt11.monthOfYear().getDurationField();
    assertEquals(COPTIC_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));
}
 
源代码24 项目: astor   文件: AssembledChronology.java
public final DurationField millis() {
    return iMillis;
}
 
源代码25 项目: coming   文件: Time_18_GJChronology_t.java
LinkedDurationField(DurationField durationField, ImpreciseCutoverField dateTimeField) {
    super(durationField, durationField.getType());
    iField = dateTimeField;
}
 
源代码26 项目: astor   文件: TestPreciseDateTimeField.java
public DurationField getDurationField() {
    return ISOChronology.getInstanceUTC().seconds();
}
 
源代码27 项目: astor   文件: ImpreciseDateTimeField.java
public final DurationField getDurationField() {
    return iDurationField;
}
 
源代码28 项目: astor   文件: TestGJChronology.java
public DurationField weeks() {
    return weekOfWeekyear().getDurationField();
}
 
源代码29 项目: astor   文件: GJChronology.java
public DurationField getLeapDurationField() {
    return iGregorianField.getLeapDurationField();
}
 
源代码30 项目: astor   文件: BasicMonthOfYearDateTimeField.java
public DurationField getRangeDurationField() {
    return iChronology.years();
}
 
 类所在包
 同包方法