Search in sources :

Example 1 with DateIntervalFormat

use of android.icu.text.DateIntervalFormat in project j2objc by google.

the class DateIntervalFormatTest method testGetInstance_String_DateIntervalInfo.

@Test
public void testGetInstance_String_DateIntervalInfo() {
    DateIntervalInfo dateIntervalInfo = new DateIntervalInfo(new ULocale("ca"));
    DateIntervalFormat dateIntervalFormat = DateIntervalFormat.getInstance(DateFormat.YEAR_MONTH, Locale.ENGLISH, dateIntervalInfo);
    Calendar from = Calendar.getInstance();
    from.set(2000, Calendar.JANUARY, 1, 12, 0);
    Calendar to = Calendar.getInstance();
    to.set(2001, Calendar.FEBRUARY, 1, 12, 0);
    DateInterval interval = new DateInterval(from.getTimeInMillis(), to.getTimeInMillis());
    dateIntervalFormat.setTimeZone(from.getTimeZone());
    // Month names are default (English), format is Catalan
    assertEquals("Wrong date interval", "January de 2000 – February de 2001", dateIntervalFormat.format(interval));
}
Also used : ULocale(android.icu.util.ULocale) DateIntervalInfo(android.icu.text.DateIntervalInfo) Calendar(android.icu.util.Calendar) DateIntervalFormat(android.icu.text.DateIntervalFormat) DateInterval(android.icu.util.DateInterval) Test(org.junit.Test)

Example 2 with DateIntervalFormat

use of android.icu.text.DateIntervalFormat in project j2objc by google.

the class DateIntervalFormatTest method TestTicket9919Setter.

@Test
public void TestTicket9919Setter() {
    // Creating a DateIntervalFormat with a custom DateIntervalInfo
    // object used to corrupt the cache.
    DateIntervalFormat dif = DateIntervalFormat.getInstance("yMd", ULocale.ENGLISH);
    Calendar from = Calendar.getInstance();
    Calendar to = Calendar.getInstance();
    from.set(2013, 3, 26);
    to.set(2013, 3, 28);
    // Save. This is the correct answer
    String expected = dif.format(from, to, new StringBuffer(), new FieldPosition(0)).toString();
    // Now create a DateIntervalFormat with same skeleton and
    // locale, but with a custom DateIntervalInfo. This used
    // to corrupt the cache.
    DateIntervalInfo dateIntervalInfo = new DateIntervalInfo(ULocale.ENGLISH);
    dateIntervalInfo.setIntervalPattern("yMd", Calendar.DATE, "M/d/y \u2013 d");
    DateIntervalFormat bad = DateIntervalFormat.getInstance("yMd", ULocale.ENGLISH);
    bad.setDateIntervalInfo(dateIntervalInfo);
    // Now create a DateIntervalFormat with same skeleton and
    // locale, but with default DateIntervalInfo. The cache should
    // not be corrupted, and we should get the same answer as before.
    dif = DateIntervalFormat.getInstance("yMd", ULocale.ENGLISH);
    assertEquals("Custom DateIntervalInfo objects should not mess up cache", expected, dif.format(from, to, new StringBuffer(), new FieldPosition(0)).toString());
}
Also used : DateIntervalInfo(android.icu.text.DateIntervalInfo) Calendar(android.icu.util.Calendar) DateIntervalFormat(android.icu.text.DateIntervalFormat) FieldPosition(java.text.FieldPosition) Test(org.junit.Test)

Example 3 with DateIntervalFormat

use of android.icu.text.DateIntervalFormat in project j2objc by google.

the class DateIntervalFormatTest method TestTicket11669.

// TestTicket11669 - Check the thread safety of DateIntervalFormat.format().
// This test failed with ICU 56.
@Test
public void TestTicket11669() {
    // These final variables are accessed directly by the concurrent threads.
    final DateIntervalFormat formatter = DateIntervalFormat.getInstance(DateFormat.YEAR_MONTH_DAY, ULocale.US);
    final ArrayList<DateInterval> testIntervals = new ArrayList<DateInterval>();
    final ArrayList<String> expectedResults = new ArrayList<String>();
    // Create and save the input test data.
    TimeZone tz = TimeZone.getTimeZone("Americal/Los_Angeles");
    Calendar intervalStart = Calendar.getInstance(tz, ULocale.US);
    Calendar intervalEnd = Calendar.getInstance(tz, ULocale.US);
    intervalStart.set(2009, 6, 1);
    intervalEnd.set(2009, 6, 2);
    testIntervals.add(new DateInterval(intervalStart.getTimeInMillis(), intervalEnd.getTimeInMillis()));
    intervalStart.set(2015, 2, 27);
    intervalEnd.set(2015, 3, 1);
    testIntervals.add(new DateInterval(intervalStart.getTimeInMillis(), intervalEnd.getTimeInMillis()));
    // Run the formatter single-threaded to create and save the expected results.
    for (DateInterval interval : testIntervals) {
        FieldPosition pos = new FieldPosition(0);
        StringBuffer result = new StringBuffer();
        formatter.format(interval, result, pos);
        expectedResults.add(result.toString());
    }
    class TestThread extends Thread {

        public String errorMessage;

        public void run() {
            for (int loop = 0; loop < 2000; ++loop) {
                ListIterator<String> expectedItr = expectedResults.listIterator();
                for (DateInterval interval : testIntervals) {
                    String expected = expectedItr.next();
                    FieldPosition pos = new FieldPosition(0);
                    StringBuffer result = new StringBuffer();
                    formatter.format(interval, result, pos);
                    if (!expected.equals(result.toString())) {
                        // Note: The ICU test framework doesn't support reporting failures from within a sub-thread.
                        // Save the failure for the main thread to pick up later.
                        errorMessage = String.format("Expected \"%s\", actual \"%s\"", expected, result);
                        return;
                    }
                }
            }
        }
    }
    List<TestThread> threads = new ArrayList<TestThread>();
    for (int i = 0; i < 4; ++i) {
        threads.add(new TestThread());
    }
    for (Thread t : threads) {
        t.start();
    }
    for (TestThread t : threads) {
        try {
            t.join();
        } catch (InterruptedException e) {
            fail("Unexpected exception: " + e.toString());
        }
        if (t.errorMessage != null) {
            fail(t.errorMessage);
        }
    }
}
Also used : Calendar(android.icu.util.Calendar) DateIntervalFormat(android.icu.text.DateIntervalFormat) ArrayList(java.util.ArrayList) DateInterval(android.icu.util.DateInterval) FieldPosition(java.text.FieldPosition) TimeZone(android.icu.util.TimeZone) Test(org.junit.Test)

Example 4 with DateIntervalFormat

use of android.icu.text.DateIntervalFormat in project j2objc by google.

the class DateIntervalFormatTest method expectUserDII.

private void expectUserDII(String[] data, int data_length) {
    int i = 1;
    while (i < data_length) {
        String locName = data[i++];
        ULocale loc = new ULocale(locName);
        SimpleDateFormat ref = new SimpleDateFormat(data[0], loc);
        // 'f'
        String datestr = data[i++];
        String datestr_2 = data[i++];
        Date date, date_2;
        try {
            date = ref.parse(datestr);
            date_2 = ref.parse(datestr_2);
        } catch (ParseException e) {
            errln("parse exception" + e);
            continue;
        }
        DateInterval dtitv = new DateInterval(date.getTime(), date_2.getTime());
        DateIntervalInfo dtitvinf = new DateIntervalInfo();
        dtitvinf.setFallbackIntervalPattern("{0} --- {1}");
        dtitvinf.setIntervalPattern("yMMMd", Calendar.MONTH, "yyyy MMM d - MMM y");
        dtitvinf.setIntervalPattern("yMMMd", Calendar.HOUR_OF_DAY, "yyyy MMM d HH:mm - HH:mm");
        DateIntervalFormat dtitvfmt = DateIntervalFormat.getInstance(DateFormat.YEAR_ABBR_MONTH_DAY, loc, dtitvinf);
        String expected = data[i++];
        String formatted = dtitvfmt.format(dtitv);
        if (!formatted.equals(Utility.unescape(expected))) {
            errln("userDII: \"" + locName + "\\" + datestr + "\\" + datestr_2 + "\"\t expected: " + expected + "\tgot: " + formatted + "\n");
        }
    }
}
Also used : ULocale(android.icu.util.ULocale) DateIntervalInfo(android.icu.text.DateIntervalInfo) DateIntervalFormat(android.icu.text.DateIntervalFormat) ParseException(java.text.ParseException) DateInterval(android.icu.util.DateInterval) SimpleDateFormat(android.icu.text.SimpleDateFormat) Date(java.util.Date)

Example 5 with DateIntervalFormat

use of android.icu.text.DateIntervalFormat in project j2objc by google.

the class DateIntervalFormatTest method TestGetSetTimeZone.

@Test
public void TestGetSetTimeZone() {
    DateIntervalFormat dtitvfmt = DateIntervalFormat.getInstance("MMMdHHmm", Locale.ENGLISH);
    // 2011-Mar-02 1030 in US/Pacific, 2011-Mar-03 0330 in Asia/Tokyo
    long date1 = 1299090600000L;
    // 2011-Mar-02 1730 in US/Pacific, 2011-Mar-03 1030 in Asia/Tokyo
    long date2 = 1299115800000L;
    DateInterval dtitv = new DateInterval(date1, date2);
    TimeZone tzCalif = TimeZone.getFrozenTimeZone("US/Pacific");
    TimeZone tzTokyo = TimeZone.getFrozenTimeZone("Asia/Tokyo");
    // ICU4C result is "Mar 2, 10:30 \u2013 17:30" (does not duplicate day)
    String fmtCalif = "Mar 2, 10:30 \u2013 17:30";
    // ICU4C result is "Mar 3, 03:30 \u2013 10:30" (does not duplicate day)
    String fmtTokyo = "Mar 3, 03:30 \u2013 10:30";
    StringBuffer buf = new StringBuffer();
    FieldPosition pos = new FieldPosition(0);
    dtitvfmt.setTimeZone(tzCalif);
    dtitvfmt.format(dtitv, buf, pos);
    if (!buf.toString().equals(fmtCalif)) {
        errln("DateIntervalFormat for tzCalif, expect \"" + fmtCalif + "\", get \"" + buf + "\"");
    }
    buf.setLength(0);
    pos.setBeginIndex(0);
    dtitvfmt.setTimeZone(tzTokyo);
    dtitvfmt.format(dtitv, buf, pos);
    if (!buf.toString().equals(fmtTokyo)) {
        errln("DateIntervalFormat for tzTokyo, expect \"" + fmtTokyo + "\", get \"" + buf + "\"");
    }
    if (!dtitvfmt.getTimeZone().equals(tzTokyo)) {
        errln("DateIntervalFormat.getTimeZone() returns mismatch");
    }
}
Also used : TimeZone(android.icu.util.TimeZone) DateIntervalFormat(android.icu.text.DateIntervalFormat) DateInterval(android.icu.util.DateInterval) FieldPosition(java.text.FieldPosition) Test(org.junit.Test)

Aggregations

DateIntervalFormat (android.icu.text.DateIntervalFormat)14 DateInterval (android.icu.util.DateInterval)11 Test (org.junit.Test)10 DateIntervalInfo (android.icu.text.DateIntervalInfo)9 Calendar (android.icu.util.Calendar)9 FieldPosition (java.text.FieldPosition)8 ULocale (android.icu.util.ULocale)7 SimpleDateFormat (android.icu.text.SimpleDateFormat)5 ParseException (java.text.ParseException)5 Date (java.util.Date)4 TimeZone (android.icu.util.TimeZone)3 DateFormat (android.icu.text.DateFormat)2 PatternInfo (android.icu.text.DateIntervalInfo.PatternInfo)1 Output (android.icu.util.Output)1 ParsePosition (java.text.ParsePosition)1 ArrayList (java.util.ArrayList)1