// -----------------------------------------------------------------------
  public void testWithMillis_long() {
    DateTime test = new DateTime(TEST_TIME1);
    DateTime result = test.withMillis(TEST_TIME2);
    assertEquals(TEST_TIME2, result.getMillis());
    assertEquals(test.getChronology(), result.getChronology());

    test = new DateTime(TEST_TIME1, GREGORIAN_PARIS);
    result = test.withMillis(TEST_TIME2);
    assertEquals(TEST_TIME2, result.getMillis());
    assertEquals(test.getChronology(), result.getChronology());

    test = new DateTime(TEST_TIME1);
    result = test.withMillis(TEST_TIME1);
    assertSame(test, result);
  }
  public void testWithZone_DateTimeZone() {
    DateTime test = new DateTime(TEST_TIME1);
    DateTime result = test.withZone(PARIS);
    assertEquals(test.getMillis(), result.getMillis());
    assertEquals(ISO_PARIS, result.getChronology());

    test = new DateTime(TEST_TIME1, GREGORIAN_PARIS);
    result = test.withZone(null);
    assertEquals(test.getMillis(), result.getMillis());
    assertEquals(GREGORIAN_DEFAULT, result.getChronology());

    test = new DateTime(TEST_TIME1);
    result = test.withZone(null);
    assertSame(test, result);
  }
  // -----------------------------------------------------------------------
  public void testGetters() {
    DateTime test = new DateTime();

    assertEquals(ISO_DEFAULT, test.getChronology());
    assertEquals(LONDON, test.getZone());
    assertEquals(TEST_TIME_NOW, test.getMillis());

    assertEquals(1, test.getEra());
    assertEquals(20, test.getCenturyOfEra());
    assertEquals(2, test.getYearOfCentury());
    assertEquals(2002, test.getYearOfEra());
    assertEquals(2002, test.getYear());
    assertEquals(6, test.getMonthOfYear());
    assertEquals(9, test.getDayOfMonth());
    assertEquals(2002, test.getWeekyear());
    assertEquals(23, test.getWeekOfWeekyear());
    assertEquals(7, test.getDayOfWeek());
    assertEquals(160, test.getDayOfYear());
    assertEquals(1, test.getHourOfDay());
    assertEquals(0, test.getMinuteOfHour());
    assertEquals(60, test.getMinuteOfDay());
    assertEquals(0, test.getSecondOfMinute());
    assertEquals(60 * 60, test.getSecondOfDay());
    assertEquals(0, test.getMillisOfSecond());
    assertEquals(60 * 60 * 1000, test.getMillisOfDay());
  }
 public void testToMutableDateTimeISO() {
   DateTime test = new DateTime(TEST_TIME1, PARIS);
   MutableDateTime result = test.toMutableDateTimeISO();
   assertSame(MutableDateTime.class, result.getClass());
   assertSame(ISOChronology.class, result.getChronology().getClass());
   assertEquals(test.getMillis(), result.getMillis());
   assertEquals(ISO_PARIS, result.getChronology());
 }
  public void testToDateTime_Chronology() {
    DateTime test = new DateTime(TEST_TIME1);
    DateTime result = test.toDateTime(ISO_DEFAULT);
    assertSame(test, result);

    test = new DateTime(TEST_TIME1);
    result = test.toDateTime(GREGORIAN_PARIS);
    assertEquals(test.getMillis(), result.getMillis());
    assertEquals(GREGORIAN_PARIS, result.getChronology());

    test = new DateTime(TEST_TIME1, GREGORIAN_PARIS);
    result = test.toDateTime((Chronology) null);
    assertEquals(test.getMillis(), result.getMillis());
    assertEquals(ISO_DEFAULT, result.getChronology());

    test = new DateTime(TEST_TIME1);
    result = test.toDateTime((Chronology) null);
    assertSame(test, result);
  }
  public void testToDateTime_DateTimeZone() {
    DateTime test = new DateTime(TEST_TIME1);
    DateTime result = test.toDateTime(LONDON);
    assertSame(test, result);

    test = new DateTime(TEST_TIME1);
    result = test.toDateTime(PARIS);
    assertEquals(test.getMillis(), result.getMillis());
    assertEquals(PARIS, result.getZone());

    test = new DateTime(TEST_TIME1, PARIS);
    result = test.toDateTime((DateTimeZone) null);
    assertEquals(test.getMillis(), result.getMillis());
    assertEquals(LONDON, result.getZone());

    test = new DateTime(TEST_TIME1);
    result = test.toDateTime((DateTimeZone) null);
    assertSame(test, result);
  }
  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 testToMutableDateTime_DateTimeZone() {
    DateTime test = new DateTime(TEST_TIME1);
    MutableDateTime result = test.toMutableDateTime(LONDON);
    assertEquals(test.getMillis(), result.getMillis());
    assertEquals(ISO_DEFAULT, result.getChronology());

    test = new DateTime(TEST_TIME1);
    result = test.toMutableDateTime(PARIS);
    assertEquals(test.getMillis(), result.getMillis());
    assertEquals(ISO_PARIS, result.getChronology());

    test = new DateTime(TEST_TIME1, PARIS);
    result = test.toMutableDateTime((DateTimeZone) null);
    assertEquals(test.getMillis(), result.getMillis());
    assertEquals(ISO_DEFAULT, result.getChronology());

    test = new DateTime(TEST_TIME1);
    result = test.toMutableDateTime((DateTimeZone) null);
    assertEquals(test.getMillis(), result.getMillis());
    assertEquals(ISO_DEFAULT, result.getChronology());
  }
  public void testToDateTimeISO() {
    DateTime test = new DateTime(TEST_TIME1);
    DateTime result = test.toDateTimeISO();
    assertSame(test, result);

    test = new DateTime(TEST_TIME1, ISO_PARIS);
    result = test.toDateTimeISO();
    assertSame(DateTime.class, result.getClass());
    assertSame(ISOChronology.class, result.getChronology().getClass());
    assertEquals(test.getMillis(), result.getMillis());
    assertEquals(ISO_PARIS, result.getChronology());
    assertNotSame(test, result);

    test = new DateTime(TEST_TIME1, BUDDHIST_DEFAULT);
    result = test.toDateTimeISO();
    assertSame(DateTime.class, result.getClass());
    assertSame(ISOChronology.class, result.getChronology().getClass());
    assertEquals(test.getMillis(), result.getMillis());
    assertEquals(ISO_DEFAULT, result.getChronology());
    assertNotSame(test, result);

    test = new DateTime(TEST_TIME1, new MockNullZoneChronology());
    result = test.toDateTimeISO();
    assertSame(DateTime.class, result.getClass());
    assertSame(ISOChronology.class, result.getChronology().getClass());
    assertEquals(test.getMillis(), result.getMillis());
    assertEquals(ISO_DEFAULT, result.getChronology());
    assertNotSame(test, result);
  }
  public void testWithTime_int_int_int() {
    DateTime test = new DateTime(TEST_TIME1 - 12345L, BUDDHIST_UTC);
    DateTime result = test.withTime(12, 24, 0, 0);
    assertEquals(TEST_TIME1, result.getMillis());
    assertEquals(BUDDHIST_UTC, result.getChronology());

    test = new DateTime(TEST_TIME1);
    try {
      test.withTime(25, 1, 1, 1);
      fail();
    } catch (IllegalArgumentException ex) {
    }
  }
  public void testMinYear() {
    final ISOChronology chrono = ISOChronology.getInstanceUTC();
    final int minYear = chrono.year().getMinimumValue();

    DateTime start = new DateTime(minYear, 1, 1, 0, 0, 0, 0, chrono);
    DateTime end = new DateTime(minYear, 12, 31, 23, 59, 59, 999, chrono);
    assertTrue(start.getMillis() < 0);
    assertTrue(end.getMillis() > start.getMillis());
    assertEquals(minYear, start.getYear());
    assertEquals(minYear, end.getYear());
    long delta = end.getMillis() - start.getMillis();
    long expectedDelta =
        (start.year().isLeap() ? 366L : 365L) * DateTimeConstants.MILLIS_PER_DAY - 1;
    assertEquals(expectedDelta, delta);

    String d = minYear + "-01-01T00:00:00.000";
    Date date = dtf.parse(d);

    assertEquals(start, new DateTime(date, chrono));
    String endStr = minYear + "-12-31T23:59:59.999";
    date = dtf.parse(endStr);

    assertEquals(end, new DateTime(date, chrono));

    try {
      start.minusYears(1);
      fail();
    } catch (IllegalFieldValueException e) {
    }

    try {
      end.minusYears(1);
      fail();
    } catch (IllegalFieldValueException e) {
    }

    assertEquals(minYear - 1, chrono.year().get(Long.MIN_VALUE));
  }
  public void testWithZoneRetainFields_DateTimeZone() {
    DateTime test = new DateTime(TEST_TIME1);
    DateTime result = test.withZoneRetainFields(PARIS);
    assertEquals(test.getMillis() - DateTimeConstants.MILLIS_PER_HOUR, result.getMillis());
    assertEquals(ISO_PARIS, result.getChronology());

    test = new DateTime(TEST_TIME1);
    result = test.withZoneRetainFields(LONDON);
    assertSame(test, result);

    test = new DateTime(TEST_TIME1);
    result = test.withZoneRetainFields(null);
    assertSame(test, result);

    test = new DateTime(TEST_TIME1, GREGORIAN_PARIS);
    result = test.withZoneRetainFields(null);
    assertEquals(test.getMillis() + DateTimeConstants.MILLIS_PER_HOUR, result.getMillis());
    assertEquals(GREGORIAN_DEFAULT, result.getChronology());

    test = new DateTime(TEST_TIME1, new MockNullZoneChronology());
    result = test.withZoneRetainFields(LONDON);
    assertSame(test, result);
  }
 public void testToDate() {
   DateTime test = new DateTime(TEST_TIME1);
   Date result = test.toDate();
   assertEquals(test.getMillis(), result.getTime());
 }