Search in sources :

Example 6 with DateIntervalInfo

use of android.icu.text.DateIntervalInfo 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 7 with DateIntervalInfo

use of android.icu.text.DateIntervalInfo 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 8 with DateIntervalInfo

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

the class DateIntervalFormatTest method TestConstructor.

@Test
public void TestConstructor() {
    DateIntervalInfo diiJapan = new DateIntervalInfo(ULocale.JAPAN);
    DateIntervalInfo diiJapanJ = new DateIntervalInfo(Locale.JAPAN);
    assertEquals("DateIntervalInfo constructors", diiJapan, diiJapanJ);
}
Also used : DateIntervalInfo(android.icu.text.DateIntervalInfo) Test(org.junit.Test)

Example 9 with DateIntervalInfo

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

the class DateIntervalFormatTest method TestTicket9914.

@Test
public void TestTicket9914() {
    DateIntervalInfo dateIntervalInfo = new DateIntervalInfo(ULocale.ENGLISH);
    Calendar from = Calendar.getInstance();
    Calendar to = Calendar.getInstance();
    from.set(113, 3, 26);
    to.set(113, 3, 28);
    DateIntervalFormat dif = DateIntervalFormat.getInstance("yyyyMd", ULocale.ENGLISH, dateIntervalInfo);
    assertEquals("yyyyMd skeleton.", "4/26/0113 \u2013 4/28/0113", dif.format(from, to, new StringBuffer(), new FieldPosition(0)).toString());
    dif = DateIntervalFormat.getInstance("yyMd", ULocale.ENGLISH, dateIntervalInfo);
    assertEquals("yyMd skeleton.", "4/26/13 \u2013 4/28/13", dif.format(from, to, new StringBuffer(), new FieldPosition(0)).toString());
    dif = DateIntervalFormat.getInstance("yMd", ULocale.ENGLISH, dateIntervalInfo);
    assertEquals("yMd skeleton.", "4/26/113 \u2013 4/28/113", 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 10 with DateIntervalInfo

use of android.icu.text.DateIntervalInfo 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

DateIntervalInfo (android.icu.text.DateIntervalInfo)17 Test (org.junit.Test)15 DateIntervalFormat (android.icu.text.DateIntervalFormat)9 ULocale (android.icu.util.ULocale)8 Calendar (android.icu.util.Calendar)7 ParseException (java.text.ParseException)7 DateInterval (android.icu.util.DateInterval)6 FieldPosition (java.text.FieldPosition)4 SimpleDateFormat (android.icu.text.SimpleDateFormat)3 DateFormat (android.icu.text.DateFormat)2 PatternInfo (android.icu.text.DateIntervalInfo.PatternInfo)2 Date (java.util.Date)2 Output (android.icu.util.Output)1 ParsePosition (java.text.ParsePosition)1