Search in sources :

Example 11 with DateIntervalInfo

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

the class DateIntervalFormatTest method TestGetIntervalPattern.

/* Tests the method
     *      public PatternInfo getIntervalPattern(String skeleton, int field)
     */
@Test
public void TestGetIntervalPattern() {
    // Tests when "if ( field > MINIMUM_SUPPORTED_CALENDAR_FIELD )" is true
    // MINIMUM_SUPPORTED_CALENDAR_FIELD = Calendar.SECOND;
    DateIntervalInfo dii = new DateIntervalInfo();
    try {
        dii.getIntervalPattern("", Calendar.SECOND + 1);
        errln("DateIntervalInfo.getIntervalPattern(String,int) was suppose " + "to return an exception for the 'int field' parameter " + "when it exceeds MINIMUM_SUPPORTED_CALENDAR_FIELD.");
    } catch (Exception e) {
    }
}
Also used : DateIntervalInfo(android.icu.text.DateIntervalInfo) ParseException(java.text.ParseException) Test(org.junit.Test)

Example 12 with DateIntervalInfo

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

the class DateIntervalFormatTest method TestTicket11583.

@Test
public void TestTicket11583() {
    ULocale[] locales = { ULocale.ENGLISH, SPANISH, LA_SPANISH };
    String[] skeletons = { "yMMMMd", "yMMMM", "MMMM", "yMMMd", "yMMM", "MMM", "yMMd", "yMMdd", "yMM", "MM", "yMdd", "yMd", "yM", "M" };
    final long startDate = 1232364615000L;
    final long endDate = 1240399815000L;
    // "yMMM";
    String filterPattern = null;
    for (ULocale locale : locales) {
        for (String skeleton : skeletons) {
            if (filterPattern != null && !skeleton.equals(filterPattern)) {
                continue;
            }
            DateFormat dateFormat = DateFormat.getPatternInstance(skeleton, locale);
            String dateFormatPattern = ((SimpleDateFormat) dateFormat).toPattern();
            DateIntervalFormat intervalFormat = DateIntervalFormat.getInstance(skeleton, locale);
            DateIntervalInfo intervalInfo = intervalFormat.getDateIntervalInfo();
            if (skeleton.equals(filterPattern)) {
                logln(filterPattern + " => " + intervalInfo.getRawPatterns().get(filterPattern));
            }
            DateInterval date_interval = new DateInterval(startDate, endDate);
            String interval = intervalFormat.format(date_interval);
            String formattedStart = dateFormat.format(startDate);
            String formattedEnd = dateFormat.format(endDate);
            PatternInfo patternInfo = intervalFormat.getRawPatterns().get("M");
            String firstPart = patternInfo.getFirstPart();
            String secondPart = patternInfo.getSecondPart();
            if (!matches(dateFormatPattern, firstPart, secondPart)) {
                if (logKnownIssue("11585", "incompatible pattern between date format and date interval format")) {
                    logln("For skeleton " + skeleton + "/locale " + locale + ": mismatch between date format «" + dateFormatPattern + "» and date interval format «" + firstPart + secondPart + "».");
                } else {
                    errln("For skeleton " + skeleton + "/locale " + locale + ": mismatch between date format «" + dateFormatPattern + "» and date interval format «" + firstPart + secondPart + "».");
                }
            }
            logln(locale + "\tskeleton: «" + skeleton + "»\tpattern: «" + dateFormatPattern + "»\tintervalPattern1: «" + firstPart + "»\tintervalPattern2: «" + secondPart + "»\tstartDate: «" + formattedStart + "»\tendDate: «" + formattedEnd + "»\tinterval: «" + interval + "»");
        }
    }
}
Also used : ULocale(android.icu.util.ULocale) DateIntervalInfo(android.icu.text.DateIntervalInfo) DateFormat(android.icu.text.DateFormat) SimpleDateFormat(android.icu.text.SimpleDateFormat) DateIntervalFormat(android.icu.text.DateIntervalFormat) DateInterval(android.icu.util.DateInterval) PatternInfo(android.icu.text.DateIntervalInfo.PatternInfo) SimpleDateFormat(android.icu.text.SimpleDateFormat) Test(org.junit.Test)

Example 13 with DateIntervalInfo

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

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

the class DateIntervalFormatTest method TestIsFrozen.

/* Tests the method
     *      public boolean isFrozen()
     */
@Test
public void TestIsFrozen() {
    DateIntervalInfo dii = new DateIntervalInfo();
    if (dii.isFrozen() != false) {
        errln("DateIntervalInfo.isFrozen() is suppose to return false.");
    }
    dii.freeze();
    if (dii.isFrozen() != true) {
        errln("DateIntervalInfo.isFrozen() is suppose to return true.");
    }
}
Also used : DateIntervalInfo(android.icu.text.DateIntervalInfo) Test(org.junit.Test)

Example 15 with DateIntervalInfo

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

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