コード例 #1
0
ファイル: CalendarTest.java プロジェクト: icu-project/icu4j
  /**
   * Iterates through a list of calendar <code>TestCase</code> objects and makes sure that the
   * time-to-fields and fields-to-time calculations work correnctly for the values in each test
   * case.
   */
  public void doTestCases(TestCase[] cases, Calendar cal) {
    cal.setTimeZone(UTC);

    // Get a format to use for printing dates in the calendar system we're testing
    DateFormat format =
        DateFormat.getDateTimeInstance(cal, DateFormat.SHORT, -1, Locale.getDefault());

    final String pattern =
        (cal instanceof ChineseCalendar)
            ? "E MMl/dd/y G HH:mm:ss.S z"
            : "E, MM/dd/yyyy G HH:mm:ss.S z";

    ((SimpleDateFormat) format).applyPattern(pattern);

    // This format is used for printing Gregorian dates.
    DateFormat gregFormat = new SimpleDateFormat(pattern);
    gregFormat.setTimeZone(UTC);

    GregorianCalendar pureGreg = new GregorianCalendar(UTC);
    pureGreg.setGregorianChange(new Date(Long.MIN_VALUE));
    DateFormat pureGregFmt = new SimpleDateFormat("E M/d/yyyy G");
    pureGregFmt.setCalendar(pureGreg);

    // Now iterate through the test cases and see what happens
    for (int i = 0; i < cases.length; i++) {
      logln("\ntest case: " + i);
      TestCase test = cases[i];

      //
      // First we want to make sure that the millis -> fields calculation works
      // test.applyTime will call setTime() on the calendar object, and
      // test.fieldsEqual will retrieve all of the field values and make sure
      // that they're the same as the ones in the testcase
      //
      test.applyTime(cal);
      if (!test.fieldsEqual(cal, this)) {
        errln(
            "Fail: (millis=>fields) "
                + gregFormat.format(test.getTime())
                + " => "
                + format.format(cal.getTime())
                + ", expected "
                + test);
      }

      //
      // If that was OK, check the fields -> millis calculation
      // test.applyFields will set all of the calendar's fields to
      // match those in the test case.
      //
      cal.clear();
      test.applyFields(cal);
      if (!test.equals(cal)) {
        errln(
            "Fail: (fields=>millis) "
                + test
                + " => "
                + pureGregFmt.format(cal.getTime())
                + ", expected "
                + pureGregFmt.format(test.getTime()));
      }
    }
  }