Search in sources :

Example 11 with DateIntervalFormat

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

the class DateIntervalFormatTest method stress.

private void stress(String[] data, int data_length, Locale loc, String locName) {
    String[] skeleton = { "EEEEdMMMMy", "dMMMMy", "dMMMM", "MMMMy", "EEEEdMMMM", "EEEdMMMy", "dMMMy", "dMMM", "MMMy", "EEEdMMM", "EEEdMy", "dMy", "dM", "My", "EEEdM", "d", "EEEd", "y", "M", "MMM", "MMMM", "hm", "hmv", "hmz", "h", "hv", "hz", // following could be normalized
    "EEddMMyyyy", "EddMMy", "hhmm", "hhmmzz", // following could not be normalized
    "hms", "dMMMMMy", "EEEEEdM" };
    int i = 0;
    SimpleDateFormat ref = new SimpleDateFormat(data[i++], loc);
    while (i < data_length) {
        // 'f'
        String datestr = data[i++];
        String datestr_2 = data[i++];
        Date 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());
        for (int skeletonIndex = 0; skeletonIndex < skeleton.length; ++skeletonIndex) {
            String oneSkeleton = skeleton[skeletonIndex];
            // need special handle of "Thai" since the default calendar
            // of "Thai" is "Budd", not "Gregorian".
            DateIntervalFormat dtitvfmt = DateIntervalFormat.getInstance(oneSkeleton, loc);
            /*
                if ( locName.equals("th") ) {
                    // reset calendar to be Gregorian
                    GregorianCalendar gregCal = new GregorianCalendar(loc);
                    DateFormat dformat = dtitvfmt.getDateFormat();
                    DateFormat newOne = (DateFormat)dformat.clone();
                    newOne.setCalendar(gregCal);
                    dtitvfmt.setDateFormat(newOne);
                }
                 */
            dtitvfmt.format(dtitv);
        }
        // test interval format by algorithm
        for (int style = DateFormat.FULL; style < 4; ++style) {
            SimpleDateFormat dtfmt = (SimpleDateFormat) DateFormat.getDateInstance(style, loc);
            FieldPosition pos = new FieldPosition(0);
            StringBuffer str = new StringBuffer("");
            Calendar fromCalendar = (Calendar) dtfmt.getCalendar().clone();
            Calendar toCalendar = (Calendar) dtfmt.getCalendar().clone();
            fromCalendar.setTimeInMillis(dtitv.getFromDate());
            toCalendar.setTimeInMillis(dtitv.getToDate());
            dtfmt.intervalFormatByAlgorithm(fromCalendar, toCalendar, str, pos);
        }
    }
}
Also used : Calendar(android.icu.util.Calendar) DateIntervalFormat(android.icu.text.DateIntervalFormat) ParseException(java.text.ParseException) DateInterval(android.icu.util.DateInterval) FieldPosition(java.text.FieldPosition) SimpleDateFormat(android.icu.text.SimpleDateFormat) Date(java.util.Date)

Example 12 with DateIntervalFormat

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

the class DateIntervalFormatTest method testGetInstance_String_Locale_DateIntervalInfo.

@Test
public void testGetInstance_String_Locale_DateIntervalInfo() {
    DateIntervalInfo dateIntervalInfo = new DateIntervalInfo(new ULocale("ca"));
    DateIntervalFormat dateIntervalFormat = DateIntervalFormat.getInstance(DateFormat.YEAR_MONTH, Locale.GERMAN, 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 German, format is Catalan
    assertEquals("Wrong date interval", "Januar de 2000 – Februar 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 13 with DateIntervalFormat

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

the class DateIntervalFormatTest method expect.

private void expect(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());
        String oneSkeleton = data[i++];
        DateIntervalFormat dtitvfmt = DateIntervalFormat.getInstance(oneSkeleton, loc);
        String expected = data[i++];
        String formatted = dtitvfmt.format(dtitv);
        if (!formatted.equals(Utility.unescape(expected))) {
            errln("\"" + locName + "\\" + oneSkeleton + "\\" + datestr + "\\" + datestr_2 + "\"\t expected: " + expected + "\tgot: " + formatted + "\n");
        }
    }
}
Also used : ULocale(android.icu.util.ULocale) DateIntervalFormat(android.icu.text.DateIntervalFormat) ParseException(java.text.ParseException) DateInterval(android.icu.util.DateInterval) SimpleDateFormat(android.icu.text.SimpleDateFormat) Date(java.util.Date)

Example 14 with DateIntervalFormat

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

the class DateIntervalFormatTest method TestTicket9919GetInstance.

@Test
public void TestTicket9919GetInstance() {
    // 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.getInstance("yMd", ULocale.ENGLISH, 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)

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