Search in sources :

Example 1 with DateInterval

use of android.icu.util.DateInterval 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 DateInterval

use of android.icu.util.DateInterval 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 3 with DateInterval

use of android.icu.util.DateInterval 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 4 with DateInterval

use of android.icu.util.DateInterval 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)

Example 5 with DateInterval

use of android.icu.util.DateInterval in project j2objc by google.

the class DateIntervalFormatTest method expectUserCLDR.

private void expectUserCLDR(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());
        DateIntervalFormat dtitvfmt = DateIntervalFormat.getInstance("yyyyMMMdd", loc);
        // DateIntervalFormat dtitvfmt = DateIntervalFormat.getInstance("yMd");
        // SimpleDateFormat dtfmt = new SimpleDateFormat("yyyy 'year' MMM 'month' dd 'day'", loc);
        // dtitvfmt.setDateFormat(dtfmt);
        DateIntervalInfo dtitvinf = new DateIntervalInfo();
        dtitvinf.setFallbackIntervalPattern("{0} --- {1}");
        dtitvinf.setIntervalPattern("yMMMd", Calendar.YEAR, "'all diff'");
        dtitvinf.setIntervalPattern("yMMMd", Calendar.MONTH, "yyyy 'diff' MMM d - MMM y");
        dtitvinf.setIntervalPattern("yMMMd", Calendar.DATE, "yyyy MMM d ~ d");
        dtitvinf.setIntervalPattern("yMMMd", Calendar.HOUR_OF_DAY, "yyyy MMMd HH:mm ~ HH:mm");
        dtitvfmt.setDateIntervalInfo(dtitvinf);
        FieldPosition pos = new FieldPosition(0);
        StringBuffer str = new StringBuffer("");
        DateFormat dtfmt = dtitvfmt.getDateFormat();
        Calendar fromCalendar = (Calendar) dtfmt.getCalendar().clone();
        Calendar toCalendar = (Calendar) dtfmt.getCalendar().clone();
        fromCalendar.setTimeInMillis(dtitv.getFromDate());
        toCalendar.setTimeInMillis(dtitv.getToDate());
        dtitvfmt.format(fromCalendar, toCalendar, str, pos);
        String expected = data[i++];
        String formatted = dtitvfmt.format(dtitv).toString();
        if (!formatted.equals(Utility.unescape(expected))) {
            errln("CLDR: \"" + locName + "\\" + datestr + "\\" + datestr_2 + "\"\t expected: " + expected + "\tgot: " + formatted + "\n");
        }
    }
}
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) FieldPosition(java.text.FieldPosition) Date(java.util.Date) DateFormat(android.icu.text.DateFormat) SimpleDateFormat(android.icu.text.SimpleDateFormat) ParseException(java.text.ParseException) SimpleDateFormat(android.icu.text.SimpleDateFormat)

Aggregations

DateIntervalFormat (android.icu.text.DateIntervalFormat)11 DateInterval (android.icu.util.DateInterval)11 ULocale (android.icu.util.ULocale)7 Test (org.junit.Test)7 DateIntervalInfo (android.icu.text.DateIntervalInfo)6 Calendar (android.icu.util.Calendar)6 SimpleDateFormat (android.icu.text.SimpleDateFormat)5 FieldPosition (java.text.FieldPosition)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