下面列出了org.joda.time.DurationField#getDifference ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Gets the values of a period from an interval.
*
* @param period the period instant to use
* @param duration the duration to query
* @return the values of the period extracted from the duration
*/
public int[] get(ReadablePeriod period, long duration) {
int size = period.size();
int[] values = new int[size];
if (duration != 0) {
long current = 0;
for (int i = 0; i < size; i++) {
DurationField field = period.getFieldType(i).getField(this);
if (field.isPrecise()) {
int value = field.getDifference(duration, current);
current = field.add(current, value);
values[i] = value;
}
}
}
return values;
}
private void testAdd(String start, DurationFieldType type, int amt, String end) {
DateTime dtStart = new DateTime(start, GJChronology.getInstance(DateTimeZone.UTC));
DateTime dtEnd = new DateTime(end, GJChronology.getInstance(DateTimeZone.UTC));
assertEquals(dtEnd, dtStart.withFieldAdded(type, amt));
assertEquals(dtStart, dtEnd.withFieldAdded(type, -amt));
DurationField field = type.getField(GJChronology.getInstance(DateTimeZone.UTC));
int diff = field.getDifference(dtEnd.getMillis(), dtStart.getMillis());
assertEquals(amt, diff);
if (type == DurationFieldType.years() ||
type == DurationFieldType.months() ||
type == DurationFieldType.days()) {
YearMonthDay ymdStart = new YearMonthDay(start, GJChronology.getInstance(DateTimeZone.UTC));
YearMonthDay ymdEnd = new YearMonthDay(end, GJChronology.getInstance(DateTimeZone.UTC));
assertEquals(ymdEnd, ymdStart.withFieldAdded(type, amt));
assertEquals(ymdStart, ymdEnd.withFieldAdded(type, -amt));
}
}
private void testAdd(String start, DurationFieldType type, int amt, String end) {
DateTime dtStart = new DateTime(start, ISOChronology.getInstanceUTC());
DateTime dtEnd = new DateTime(end, ISOChronology.getInstanceUTC());
assertEquals(dtEnd, dtStart.withFieldAdded(type, amt));
assertEquals(dtStart, dtEnd.withFieldAdded(type, -amt));
DurationField field = type.getField(ISOChronology.getInstanceUTC());
int diff = field.getDifference(dtEnd.getMillis(), dtStart.getMillis());
assertEquals(amt, diff);
if (type == DurationFieldType.years() ||
type == DurationFieldType.months() ||
type == DurationFieldType.days()) {
YearMonthDay ymdStart = new YearMonthDay(start, ISOChronology.getInstanceUTC());
YearMonthDay ymdEnd = new YearMonthDay(end, ISOChronology.getInstanceUTC());
assertEquals(ymdEnd, ymdStart.withFieldAdded(type, amt));
assertEquals(ymdStart, ymdEnd.withFieldAdded(type, -amt));
}
}
/**
* Gets the values of a period from an interval.
*
* @param period the period instant to use
* @param duration the duration to query
* @return the values of the period extracted from the duration
*/
public int[] get(ReadablePeriod period, long duration) {
int size = period.size();
int[] values = new int[size];
if (duration != 0) {
long current = 0;
for (int i = 0; i < size; i++) {
DurationField field = period.getFieldType(i).getField(this);
if (field.isPrecise()) {
int value = field.getDifference(duration, current);
current = field.add(current, value);
values[i] = value;
}
}
}
return values;
}
private void testAdd(String start, DurationFieldType type, int amt, String end) {
DateTime dtStart = new DateTime(start, GJChronology.getInstance(DateTimeZone.UTC));
DateTime dtEnd = new DateTime(end, GJChronology.getInstance(DateTimeZone.UTC));
assertEquals(dtEnd, dtStart.withFieldAdded(type, amt));
assertEquals(dtStart, dtEnd.withFieldAdded(type, -amt));
DurationField field = type.getField(GJChronology.getInstance(DateTimeZone.UTC));
int diff = field.getDifference(dtEnd.getMillis(), dtStart.getMillis());
assertEquals(amt, diff);
if (type == DurationFieldType.years() ||
type == DurationFieldType.months() ||
type == DurationFieldType.days()) {
YearMonthDay ymdStart = new YearMonthDay(start, GJChronology.getInstance(DateTimeZone.UTC));
YearMonthDay ymdEnd = new YearMonthDay(end, GJChronology.getInstance(DateTimeZone.UTC));
assertEquals(ymdEnd, ymdStart.withFieldAdded(type, amt));
assertEquals(ymdStart, ymdEnd.withFieldAdded(type, -amt));
}
}
private void testAdd(String start, DurationFieldType type, int amt, String end) {
DateTime dtStart = new DateTime(start, ISOChronology.getInstanceUTC());
DateTime dtEnd = new DateTime(end, ISOChronology.getInstanceUTC());
assertEquals(dtEnd, dtStart.withFieldAdded(type, amt));
assertEquals(dtStart, dtEnd.withFieldAdded(type, -amt));
DurationField field = type.getField(ISOChronology.getInstanceUTC());
int diff = field.getDifference(dtEnd.getMillis(), dtStart.getMillis());
assertEquals(amt, diff);
if (type == DurationFieldType.years() ||
type == DurationFieldType.months() ||
type == DurationFieldType.days()) {
YearMonthDay ymdStart = new YearMonthDay(start, ISOChronology.getInstanceUTC());
YearMonthDay ymdEnd = new YearMonthDay(end, ISOChronology.getInstanceUTC());
assertEquals(ymdEnd, ymdStart.withFieldAdded(type, amt));
assertEquals(ymdStart, ymdEnd.withFieldAdded(type, -amt));
}
}
/**
* 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;
}
/**
* 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;
}