* Date/time classes are immutable. Therefore methods to alter instances of them are named with() rather than being
* setters, and they return new instances.
*/
@Test
public void testAlterDateTimeComponentValues() {
final LocalDateTime localDateTime = LocalDateTime.of(2014, Month.AUGUST, 30, 19, 02, 58);
// Test altering component fields with literal values using withXXX()
int pastYear = 2013;
int pastDayOfMonth = 1;
final LocalDateTime pastDateTime = localDateTime.withYear(pastYear).withDayOfMonth(pastDayOfMonth);
// Construct separate LocalDate and Time from supplied component values, and time component of existing DateTime
LocalDate expectedDate = LocalDate.of(pastYear, localDateTime.getMonth(), pastDayOfMonth);
final LocalTime expectedTime = localDateTime.toLocalTime();
// A LocalDateTime can be constructed from separate LocalDate and LocalTime
assertThat(pastDateTime, is(LocalDateTime.of(expectedDate, expectedTime)));
// Test altering component fields using delta method plusXXX()
LocalDateTime futureDateTime = localDateTime.plusWeeks(3);
expectedDate = LocalDate.of(localDateTime.getYear(), Month.SEPTEMBER, 20);
assertThat(futureDateTime, is(LocalDateTime.of(expectedDate, expectedTime)));
// Test altering component fields using generic delta method plus(value, Unit)
futureDateTime = futureDateTime.plus(1, ChronoUnit.WEEKS);
expectedDate = LocalDate.of(localDateTime.getYear(), Month.SEPTEMBER, 27);
assertThat(futureDateTime, is(LocalDateTime.of(expectedDate, expectedTime)));
}