// ----------------------------------------------------------------------- public void testWithFieldAdded1() { DateTime test = new DateTime(2004, 6, 9, 0, 0, 0, 0); DateTime result = test.withFieldAdded(DurationFieldType.years(), 6); assertEquals(new DateTime(2004, 6, 9, 0, 0, 0, 0), test); assertEquals(new DateTime(2010, 6, 9, 0, 0, 0, 0), result); }
/** Private method called from constructor. */ private void setPeriodInternal( int years, int months, int weeks, int days, int hours, int minutes, int seconds, int millis) { int[] newValues = new int[size()]; checkAndUpdate(DurationFieldType.years(), newValues, years); checkAndUpdate(DurationFieldType.months(), newValues, months); checkAndUpdate(DurationFieldType.weeks(), newValues, weeks); checkAndUpdate(DurationFieldType.days(), newValues, days); checkAndUpdate(DurationFieldType.hours(), newValues, hours); checkAndUpdate(DurationFieldType.minutes(), newValues, minutes); checkAndUpdate(DurationFieldType.seconds(), newValues, seconds); checkAndUpdate(DurationFieldType.millis(), newValues, millis); iValues = newValues; }
private void testAdd(Date start, DurationFieldType type, int amt, Date 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()) { LocalDate ymdStart = new LocalDate(start, ISOChronology.getInstanceUTC()); LocalDate ymdEnd = new LocalDate(end, ISOChronology.getInstanceUTC()); assertEquals(ymdEnd, ymdStart.withFieldAdded(type, amt)); assertEquals(ymdStart, ymdEnd.withFieldAdded(type, -amt)); } }
public void testWithFieldAdded4() { DateTime test = new DateTime(2004, 6, 9, 0, 0, 0, 0); DateTime result = test.withFieldAdded(DurationFieldType.years(), 0); assertSame(test, result); }
public void testCutoverAddYears() { DateTimeFormat format = DateTimeFormat.getFormat("YYYY-MM-DD"); Date startDate = format.parse("1582-01-01"); Date endDate = format.parse("1583-01-01"); testAdd(startDate, DurationFieldType.years(), 1, endDate); startDate = format.parse("1582-02-15"); endDate = format.parse("1583-02-28"); testAdd(startDate, DurationFieldType.years(), 1, endDate); startDate = format.parse("1582-02-28"); endDate = format.parse("1583-02-28"); testAdd(startDate, DurationFieldType.years(), 1, endDate); startDate = format.parse("1582-03-01"); endDate = format.parse("1583-03-01"); testAdd(startDate, DurationFieldType.years(), 1, endDate); startDate = format.parse("1582-09-30"); endDate = format.parse("1583-09-30"); testAdd(startDate, DurationFieldType.years(), 1, endDate); startDate = format.parse("1582-10-01"); endDate = format.parse("1583-10-01"); testAdd(startDate, DurationFieldType.years(), 1, endDate); startDate = format.parse("1582-10-04"); endDate = format.parse("1583-10-04"); testAdd(startDate, DurationFieldType.years(), 1, endDate); startDate = format.parse("1582-10-15"); endDate = format.parse("1583-10-15"); testAdd(startDate, DurationFieldType.years(), 1, endDate); startDate = format.parse("1582-10-16"); endDate = format.parse("1583-10-16"); testAdd(startDate, DurationFieldType.years(), 1, endDate); startDate = format.parse("1580-01-01"); endDate = format.parse("1584-01-01"); testAdd(startDate, DurationFieldType.years(), 4, endDate); startDate = format.parse("1580-02-29"); endDate = format.parse("1584-02-29"); testAdd(startDate, DurationFieldType.years(), 4, endDate); startDate = format.parse("1580-10-01"); endDate = format.parse("1584-10-01"); testAdd(startDate, DurationFieldType.years(), 4, endDate); startDate = format.parse("1580-10-10"); endDate = format.parse("1584-10-10"); testAdd(startDate, DurationFieldType.years(), 4, endDate); startDate = format.parse("1580-10-15"); endDate = format.parse("1584-10-15"); testAdd(startDate, DurationFieldType.years(), 4, endDate); startDate = format.parse("1580-12-31"); endDate = format.parse("1584-12-31"); testAdd(startDate, DurationFieldType.years(), 4, endDate); }