org.joda.time.DateTimeField Java Examples

The following examples show how to use org.joda.time.DateTimeField. 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: Time_18_GJChronology_t.java    From coming with MIT License 6 votes vote down vote up
/**
 * @param julianField field from the chronology used before the cutover instant
 * @param gregorianField field from the chronology used at and after the cutover
 * @param cutoverMillis  the millis of the cutover
 * @param convertByWeekyear
 */
CutoverField(DateTimeField julianField, DateTimeField gregorianField,
             long cutoverMillis, boolean convertByWeekyear) {
    super(gregorianField.getType());
    iJulianField = julianField;
    iGregorianField = gregorianField;
    iCutover = cutoverMillis;
    iConvertByWeekyear = convertByWeekyear;
    // Although average length of Julian and Gregorian years differ,
    // use the Gregorian duration field because it is more accurate.
    iDurationField = gregorianField.getDurationField();

    DurationField rangeField = gregorianField.getRangeDurationField();
    if (rangeField == null) {
        rangeField = julianField.getRangeDurationField();
    }
    iRangeDurationField = rangeField;
}
 
Example #2
Source File: MainTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
private void failMillis(DateTimeField fieldA, DateTimeField fieldB,
                        String method, long millis, long millisA, long millisB,
                        int valueA, int valueB) {
    System.out.println("Failure on " + makeName(fieldA, fieldB) + "." + method);
    System.out.println(fieldA.getClass().getName() + "\n\tvs. "
                       + fieldB.getClass().getName());
    System.out.println("Datetime: " + makeDatetime(millis));
    System.out.println("Millis from 1970: " + millis);
    System.out.println(makeDatetime(millisA) + " != " + makeDatetime(millisB));
    System.out.println(millisA + " != " + millisB);
    System.out.println("Original value as reported by first field: " +
                       fieldA.get(millis));
    System.out.println("Original value as reported by second field: " +
                       fieldB.get(millis));
    System.out.println("First new value as reported by first field: " +
                       fieldA.get(millisA));
    System.out.println("First new value as reported by second field: " +
                       fieldB.get(millisA));
    System.out.println("Second new value as reported by first field: " +
                       fieldA.get(millisB));
    System.out.println("Second new value as reported by second field: " +
                       fieldB.get(millisB));
    System.out.println("Value to set for first field: " + valueA);
    System.out.println("Value to set for second field: " + valueB);
    throw new RuntimeException();
}
 
Example #3
Source File: RemainderDateTimeField.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 type  the field type this field actually uses
 * @param divisor  divisor, such as 100 years in a century
 * @throws IllegalArgumentException if divisor is less than two
 */
public RemainderDateTimeField(DateTimeField field,
                              DateTimeFieldType type, int divisor) {
    super(field, type);

    if (divisor < 2) {
        throw new IllegalArgumentException("The divisor must be at least 2");
    }

    DurationField rangeField = field.getDurationField();
    if (rangeField == null) {
        iRangeField = null;
    } else {
        iRangeField = new ScaledDurationField(
            rangeField, type.getRangeDurationType(), divisor);
    }
    iDurationField = field.getDurationField();
    iDivisor = divisor;
}
 
Example #4
Source File: ZonedChronology.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
ZonedDateTimeField(DateTimeField field,
                   DateTimeZone zone,
                   DurationField durationField,
                   DurationField rangeDurationField,
                   DurationField leapDurationField) {
    super(field.getType());
    if (!field.isSupported()) {
        throw new IllegalArgumentException();
    }
    iField = field;
    iZone = zone;
    iDurationField = durationField;
    iTimeField = useTimeArithmetic(durationField);
    iRangeDurationField = rangeDurationField;
    iLeapDurationField = leapDurationField;
}
 
Example #5
Source File: Time_20_DateTimeFormatterBuilder_s.java    From coming with MIT License 5 votes vote down vote up
public void printTo(
        Writer out, long instant, Chronology chrono,
        int displayOffset, DateTimeZone displayZone, Locale locale) throws IOException {
    try {
        DateTimeField field = iFieldType.getField(chrono);
        FormatUtils.writeUnpaddedInteger(out, field.get(instant));
    } catch (RuntimeException e) {
        out.write('\ufffd');
    }
}
 
Example #6
Source File: DecoratedDateTimeField.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Constructor.
 * 
 * @param field  the field being decorated
 * @param type  allow type to be overridden
 */
protected DecoratedDateTimeField(DateTimeField field, DateTimeFieldType type) {
    super(type);
    if (field == null) {
        throw new IllegalArgumentException("The field must not be null");
    }
    if (!field.isSupported()) {
        throw new IllegalArgumentException("The field must be supported");
    }
    iField = field;
}
 
Example #7
Source File: AcademicTrimesterDateTimeFieldType.java    From fenixedu-academic with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
public DateTimeField getField(Chronology chronology) {
    if (chronology instanceof AcademicChronology) {
        return ((AcademicChronology) chronology).academicYear();
    }
    throw unsupported();
}
 
Example #8
Source File: Nopol2017_0091_s.java    From coming with MIT License 5 votes vote down vote up
/**
 * The field with the longer range duration is ordered first, where
 * null is considered infinite. If the ranges match, then the field
 * with the longer duration is ordered first.
 */
public int compareTo(SavedField obj) {
    DateTimeField other = obj.iField;
    int result = compareReverse
        (iField.getRangeDurationField(), other.getRangeDurationField());
    if (result != 0) {
        return result;
    }
    return compareReverse
        (iField.getDurationField(), other.getDurationField());
}
 
Example #9
Source File: MainTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
private String makeName(DateTimeField fieldA, DateTimeField fieldB) {
    if (fieldA.getName().equals(fieldB.getName())) {
        return fieldA.getName();
    } else {
        return fieldA.getName() + "/" + fieldB.getName();
    }
}
 
Example #10
Source File: StrictDateTimeField.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Returns a strict version of the given field. If it is already strict,
 * then it is returned as-is. Otherwise, a new StrictDateTimeField is
 * returned.
 */
public static DateTimeField getInstance(DateTimeField field) {
    if (field == null) {
        return null;
    }
    if (field instanceof LenientDateTimeField) {
        field = ((LenientDateTimeField)field).getWrappedField();
    }
    if (!field.isLenient()) {
        return field;
    }
    return new StrictDateTimeField(field);
}
 
Example #11
Source File: DividedDateTimeField.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Construct a DividedDateTimeField that compliments the given
 * RemainderDateTimeField.
 *
 * @param remainderField  complimentary remainder field, like "yearOfCentury()".
 * @param rangeField  the range field, null to derive
 * @param type  the field type this field will actually use
 */
public DividedDateTimeField(RemainderDateTimeField remainderField, DurationField rangeField, DateTimeFieldType type) {
    super(remainderField.getWrappedField(), type);
    int divisor = iDivisor = remainderField.iDivisor;
    iDurationField = remainderField.iRangeField;
    iRangeDurationField = rangeField;
    DateTimeField field = getWrappedField();
    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 #12
Source File: DateTimeFormatterBuilder.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
public void printTo(
        StringBuffer buf, long instant, Chronology chrono,
        int displayOffset, DateTimeZone displayZone, Locale locale) {
    try {
        DateTimeField field = iFieldType.getField(chrono);
        FormatUtils.appendUnpaddedInteger(buf, field.get(instant));
    } catch (RuntimeException e) {
        buf.append('\ufffd');
    }
}
 
Example #13
Source File: TestUnsupportedDateTimeField.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * The getName() method should return the same value as the getName() method
 * of the DateTimeFieldType that was used to create the instance.
 * 
 */
public void testPublicGetNameMethod() {
    DateTimeField fieldOne = UnsupportedDateTimeField.getInstance(
            dateTimeFieldTypeOne, UnsupportedDurationField
                    .getInstance(weeks));

    assertSame(fieldOne.getName(), dateTimeFieldTypeOne.getName());
}
 
Example #14
Source File: TestUnsupportedDateTimeField.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * The getName() method should return the same value as the getName() method
 * of the DateTimeFieldType that was used to create the instance.
 * 
 */
public void testPublicGetNameMethod() {
    DateTimeField fieldOne = UnsupportedDateTimeField.getInstance(
            dateTimeFieldTypeOne, UnsupportedDurationField
                    .getInstance(weeks));

    assertSame(fieldOne.getName(), dateTimeFieldTypeOne.getName());
}
 
Example #15
Source File: Time_18_GJChronology_s.java    From coming with MIT License 5 votes vote down vote up
public int getMaximumValue(ReadablePartial partial, int[] values) {
    Chronology chrono = GJChronology.getInstanceUTC();
    long instant = 0L;
    for (int i = 0, isize = partial.size(); i < isize; i++) {
        DateTimeField field = partial.getFieldType(i).getField(chrono);
        if (values[i] <= field.getMaximumValue(instant)) {
            instant = field.set(instant, values[i]);
        }
    }
    return getMaximumValue(instant);
}
 
Example #16
Source File: SkipDateTimeField.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Constructor.
 * 
 * @param chronology  the chronoogy to use
 * @param field  the field to skip zero on
 * @param skip  the value to skip
 */
public SkipDateTimeField(Chronology chronology, DateTimeField field, int skip) {
    super(field);
    iChronology = chronology;
    int min = super.getMinimumValue();
    if (min < skip) {
        iMinValue = min - 1;
    } else if (min == skip) {
        iMinValue = skip + 1;
    } else {
        iMinValue = min;
    }
    iSkip = skip;
}
 
Example #17
Source File: Arja_0050_s.java    From coming with MIT License 5 votes vote down vote up
/**
 * Verify that input values are within specified bounds.
 * 
 * @param value  the value to check
 * @param lowerBound  the lower bound allowed for value
 * @param upperBound  the upper bound allowed for value
 * @throws IllegalFieldValueException if value is not in the specified bounds
 */
public static void verifyValueBounds(DateTimeField field, 
                                     int value, int lowerBound, int upperBound) {
    if ((value < lowerBound) || (value > upperBound)) {
        throw new IllegalFieldValueException
            (field.getType(), Integer.valueOf(value),
             Integer.valueOf(lowerBound), Integer.valueOf(upperBound));
    }
}
 
Example #18
Source File: Time_6_GJChronology_t.java    From coming with MIT License 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 #19
Source File: LimitChronology.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
LimitDateTimeField(DateTimeField field,
                   DurationField durationField,
                   DurationField rangeDurationField,
                   DurationField leapDurationField) {
    super(field, field.getType());
    iDurationField = durationField;
    iRangeDurationField = rangeDurationField;
    iLeapDurationField = leapDurationField;
}
 
Example #20
Source File: TestReadablePartialConverter.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
protected DateTimeField getField(int index, Chronology chrono) {
    switch (index) {
        case 0:
        return chrono.hourOfDay();
        case 1:
        return chrono.minuteOfHour();
        case 2:
        return chrono.year();
        case 3:
        return chrono.era();
    }
    return null;
}
 
Example #21
Source File: DateTimeFormatterBuilder.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
private long[] getFractionData(long fraction, DateTimeField field) {
    long rangeMillis = field.getDurationField().getUnitMillis();
    long scalar;
    int maxDigits = iMaxDigits;
    while (true) {
        switch (maxDigits) {
        default: scalar = 1L; break;
        case 1:  scalar = 10L; break;
        case 2:  scalar = 100L; break;
        case 3:  scalar = 1000L; break;
        case 4:  scalar = 10000L; break;
        case 5:  scalar = 100000L; break;
        case 6:  scalar = 1000000L; break;
        case 7:  scalar = 10000000L; break;
        case 8:  scalar = 100000000L; break;
        case 9:  scalar = 1000000000L; break;
        case 10: scalar = 10000000000L; break;
        case 11: scalar = 100000000000L; break;
        case 12: scalar = 1000000000000L; break;
        case 13: scalar = 10000000000000L; break;
        case 14: scalar = 100000000000000L; break;
        case 15: scalar = 1000000000000000L; break;
        case 16: scalar = 10000000000000000L; break;
        case 17: scalar = 100000000000000000L; break;
        case 18: scalar = 1000000000000000000L; break;
        }
        if (((rangeMillis * scalar) / scalar) == rangeMillis) {
            break;
        }
        // Overflowed: scale down.
        maxDigits--;
    }
    
    return new long[] {fraction * scalar / rangeMillis, maxDigits};
}
 
Example #22
Source File: MainTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
private String makeName(DateTimeField fieldA, DateTimeField fieldB) {
    if (fieldA.getName().equals(fieldB.getName())) {
        return fieldA.getName();
    } else {
        return fieldA.getName() + "/" + fieldB.getName();
    }
}
 
Example #23
Source File: TestGJChronology.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
public DateTimeField monthOfYear() {
    return new TestGJMonthOfYearField(this);
}
 
Example #24
Source File: GJChronology.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Creates a duration field that links back to this.
 */
ImpreciseCutoverField(DateTimeField julianField, DateTimeField gregorianField, long cutoverMillis) {
    this(julianField, gregorianField, null, cutoverMillis, false);
}
 
Example #25
Source File: LenientDateTimeField.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
protected LenientDateTimeField(DateTimeField field, Chronology base) {
    super(field);
    iBase = base;
}
 
Example #26
Source File: TestJulianChronology.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
public DateTimeField dayOfMonth() {
    return new TestJulianDayOfMonthField(this); 
}
 
Example #27
Source File: Nopol2017_0087_s.java    From coming with MIT License 4 votes vote down vote up
SavedField(DateTimeField field, String text, Locale locale) {
    iField = field;
    iValue = 0;
    iText = text;
    iLocale = locale;
}
 
Example #28
Source File: AssembledChronology.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
public final DateTimeField millisOfDay() {
    return iMillisOfDay;
}
 
Example #29
Source File: Time_18_GJChronology_s.java    From coming with MIT License 4 votes vote down vote up
/**
 * Creates a duration field that links back to this.
 */
ImpreciseCutoverField(DateTimeField julianField, DateTimeField gregorianField, long cutoverMillis) {
    this(julianField, gregorianField, null, cutoverMillis, false);
}
 
Example #30
Source File: AssembledChronology.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
public final DateTimeField halfdayOfDay() {
    return iHalfdayOfDay;
}