コード例 #1
0
  /** Basic test */
  public void TestBasics() {
    DurationFormat df;
    String expect;
    String formatted;

    df = DurationFormat.getInstance(new ULocale("it"));
    formatted = df.formatDurationFromNow(4096);
    expect = "fra quattro secondi";
    if (!expect.equals(formatted)) {
      errln("Expected " + expect + " but got " + formatted);
    } else {
      logln("format duration -> " + formatted);
    }

    formatted = df.formatDurationFromNowTo(new Date(0));
    Calendar cal = Calendar.getInstance();
    int years = cal.get(Calendar.YEAR) - 1970; // year of Date(0)
    expect = "fra " + years + " anni";
    if (!expect.equals(formatted)) {
      errln("Expected " + expect + " but got " + formatted);
    } else {
      logln("format date  -> " + formatted);
    }

    formatted = df.formatDurationFrom(1000 * 3600 * 24, new Date(0).getTime());
    expect = "fra un giorno";
    if (!expect.equals(formatted)) {
      errln("Expected " + expect + " but got " + formatted);
    } else {
      logln("format date from -> " + formatted);
    }

    formatted = df.format(new Long(1000 * 3600 * 24 * 2));
    expect = "fra due giorni";
    if (!expect.equals(formatted)) {
      errln("Expected " + expect + " but got " + formatted);
    } else {
      logln("format long obj -> " + formatted);
    }
  }
コード例 #2
0
ファイル: ICUDurationTest.java プロジェクト: dacut/juliet
  public void TestFromNowTo() {
    class TestCase {
      ULocale locale;
      int diffInSeconds;
      String expected;

      TestCase(ULocale locale, int diffInSeconds, String expected) {
        this.locale = locale;
        this.diffInSeconds = diffInSeconds;
        this.expected = expected;
      }
    }
    TestCase[] testCases = {
      new TestCase(ULocale.US, 10, "10 seconds from now"),
      new TestCase(ULocale.US, -10, "10 seconds ago"),
      new TestCase(ULocale.US, -1800, "30 minutes ago"),
      new TestCase(ULocale.US, 3600, "1 hour from now"),
      new TestCase(ULocale.US, 10000, "2 hours from now"),
      new TestCase(ULocale.US, -20000, "5 hours ago"),
      new TestCase(ULocale.FRANCE, -1800, "il y a 30 minutes"),
      new TestCase(ULocale.ITALY, 10000, "fra due ore"),
    };

    final long delayMS = 10; // Safe margin - 10 milliseconds
    // See the comments below
    for (TestCase test : testCases) {
      DurationFormat df = DurationFormat.getInstance(test.locale);
      long target = System.currentTimeMillis() + test.diffInSeconds * 1000;
      // Need some adjustment because time difference is recalculated in
      // formatDurationFromNowTo method.
      target = test.diffInSeconds > 0 ? target + delayMS : target - delayMS;
      Date d = new Date(target);
      String result = df.formatDurationFromNowTo(d);
      assertEquals(
          "TestFromNowTo (" + test.locale + ", " + test.diffInSeconds + "sec)",
          test.expected,
          result);
    }
  }