下面列出了怎么用org.joda.time.DurationField的API类实例代码及写法,或者点击链接到github查看源代码。
/**
* 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);
}
/**
* 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;
}
/**
* 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;
}
/**
* 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;
}
/**
* 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;
}
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);
}
/**
* 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;
}
/**
* 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;
}
/**
* 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;
}
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;
}
/**
* 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;
}
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);
}
/**
* 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;
}
public DurationField getDurationField() {
return iDurationField;
}
/**
* 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);
}
/** Returns null: the field has no range */
@Override
public DurationField getRangeDurationField() {
return null;
}
public DurationField getDurationField() {
return UnsupportedDurationField.getInstance(DurationFieldType.eras());
}
public final DurationField getRangeDurationField() {
return iRangeDurationField;
}
LinkedDurationField(DurationField durationField, ImpreciseCutoverField dateTimeField) {
super(durationField, durationField.getType());
iField = dateTimeField;
}
public DurationField getRangeDurationField() {
return iChronology.years();
}
public final DurationField days() {
return iDays;
}
public DurationField years() {
return year().getDurationField();
}
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));
}
public final DurationField millis() {
return iMillis;
}
LinkedDurationField(DurationField durationField, ImpreciseCutoverField dateTimeField) {
super(durationField, durationField.getType());
iField = dateTimeField;
}
public DurationField getDurationField() {
return ISOChronology.getInstanceUTC().seconds();
}
public final DurationField getDurationField() {
return iDurationField;
}
public DurationField weeks() {
return weekOfWeekyear().getDurationField();
}
public DurationField getLeapDurationField() {
return iGregorianField.getLeapDurationField();
}
public DurationField getRangeDurationField() {
return iChronology.years();
}