下面列出了怎么用org.joda.time.DateTimeField的API类实例代码及写法,或者点击链接到github查看源代码。
/**
* @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;
}
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();
}
/**
* 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;
}
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;
}
/**
* 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;
}
/**
* 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);
}
private String makeName(DateTimeField fieldA, DateTimeField fieldB) {
if (fieldA.getName().equals(fieldB.getName())) {
return fieldA.getName();
} else {
return fieldA.getName() + "/" + fieldB.getName();
}
}
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');
}
}
LimitDateTimeField(DateTimeField field,
DurationField durationField,
DurationField rangeDurationField,
DurationField leapDurationField) {
super(field, field.getType());
iDurationField = durationField;
iRangeDurationField = rangeDurationField;
iLeapDurationField = leapDurationField;
}
/**
* 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());
}
/**
* 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;
}
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);
}
/**
* 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));
}
}
/**
* 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;
}
private String makeName(DateTimeField fieldA, DateTimeField fieldB) {
if (fieldA.getName().equals(fieldB.getName())) {
return fieldA.getName();
} else {
return fieldA.getName() + "/" + fieldB.getName();
}
}
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;
}
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');
}
}
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};
}
/**
* 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());
}
@Override
public DateTimeField getField(Chronology chronology) {
if (chronology instanceof AcademicChronology) {
return ((AcademicChronology) chronology).academicYear();
}
throw unsupported();
}
/**
* 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());
}
/**
* 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;
}
/**
* Creates a duration field that links back to this.
*/
ImpreciseCutoverField(DateTimeField julianField, DateTimeField gregorianField, long cutoverMillis) {
this(julianField, gregorianField, null, cutoverMillis, false);
}
@Override
public final DateTimeField millisOfDay() {
return millisOfDay;
}
@Override
public final DateTimeField minuteOfDay() {
return minuteOfDay;
}
public final DateTimeField year() {
return iYear;
}
public final DateTimeField secondOfMinute() {
return iSecondOfMinute;
}
@Override
public abstract DateTimeField dayOfMonth();
private void testValue(DateTimeField fieldA, DateTimeField fieldB,
String method, long millis, long valueA, long valueB) {
if (valueA != valueB) {
failValue(fieldA, fieldB, method, millis, valueA, valueB);
}
}
private final DateTimeField convertField(DateTimeField field) {
return LenientDateTimeField.getInstance(field, getBase());
}