Java Code Examples for android.icu.util.Calendar#getType()

The following examples show how to use android.icu.util.Calendar#getType() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SimpleDateFormat.java    From j2objc with Apache License 2.0 5 votes vote down vote up
private static synchronized String getDefaultPattern() {
    ULocale defaultLocale = ULocale.getDefault(Category.FORMAT);
    if (!defaultLocale.equals(cachedDefaultLocale)) {
        cachedDefaultLocale = defaultLocale;
        Calendar cal = Calendar.getInstance(cachedDefaultLocale);

        try {
            // Load the calendar data directly.
            ICUResourceBundle rb = (ICUResourceBundle) UResourceBundle.getBundleInstance(
                    ICUData.ICU_BASE_NAME, cachedDefaultLocale);
            String resourcePath = "calendar/" + cal.getType() + "/DateTimePatterns";
            ICUResourceBundle patternsRb= rb.findWithFallback(resourcePath);

            if (patternsRb == null) {
                patternsRb = rb.findWithFallback("calendar/gregorian/DateTimePatterns");
            }
            if (patternsRb == null || patternsRb.getSize() < 9) {
                cachedDefaultPattern = FALLBACKPATTERN;
            } else {
                int defaultIndex = 8;
                if (patternsRb.getSize() >= 13) {
                    defaultIndex += (SHORT + 1);
                }
                String basePattern = patternsRb.getString(defaultIndex);

                cachedDefaultPattern = SimpleFormatterImpl.formatRawPattern(
                        basePattern, 2, 2,
                        patternsRb.getString(SHORT), patternsRb.getString(SHORT + 4));
            }
        } catch (MissingResourceException e) {
            cachedDefaultPattern = FALLBACKPATTERN;
        }
    }
    return cachedDefaultPattern;
}
 
Example 2
Source File: RelativeDateFormat.java    From j2objc with Apache License 2.0 5 votes vote down vote up
private MessageFormat initializeCombinedFormat(Calendar cal, ULocale locale) {
    String pattern;
    ICUResourceBundle rb = (ICUResourceBundle) UResourceBundle.getBundleInstance(
        ICUData.ICU_BASE_NAME, locale);
    String resourcePath = "calendar/" + cal.getType() + "/DateTimePatterns";
    ICUResourceBundle patternsRb= rb.findWithFallback(resourcePath);
    if (patternsRb == null && !cal.getType().equals("gregorian")) {
        // Try again with gregorian, if not already attempted.
        patternsRb = rb.findWithFallback("calendar/gregorian/DateTimePatterns");
    }

    if (patternsRb == null || patternsRb.getSize() < 9) {
        // Undefined or too few elements.
        pattern = "{1} {0}";
    } else {
        int glueIndex = 8;
        if (patternsRb.getSize() >= 13) {
          if (fDateStyle >= DateFormat.FULL && fDateStyle <= DateFormat.SHORT) {
              glueIndex += fDateStyle + 1;
          } else
              if (fDateStyle >= DateFormat.RELATIVE_FULL &&
                  fDateStyle <= DateFormat.RELATIVE_SHORT) {
                  glueIndex += fDateStyle + 1 - DateFormat.RELATIVE;
              }
        }
        int elementType = patternsRb.get(glueIndex).getType();
        if (elementType == UResourceBundle.ARRAY) {
            pattern = patternsRb.get(glueIndex).getString(0);
        } else {
            pattern = patternsRb.getString(glueIndex);
        }
    }
    combinedFormatHasDateAtStart = pattern.startsWith("{1}");
    fCombinedFormat = new MessageFormat(pattern, locale);
    return fCombinedFormat;
}
 
Example 3
Source File: IBMCalendarTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * Default calendar for Thai (Ticket#6302)
 */
@Test
public void TestThaiDefault() {
    // Buddhist calendar is used as the default calendar for
    // Thai locale
    Calendar cal = Calendar.getInstance(new ULocale("th_TH"));
    String type = cal.getType();
    // Android patch: Force default Gregorian calendar.
    if (!type.equals("gregorian")) {
        errln("FAIL: Gregorian calendar is not returned for locale " + cal.toString());
    }
    // Android patch end.
}
 
Example 4
Source File: CalendarRegressionTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
@Test
public void TestCalendarType6816() {
    Locale loc = new Locale("en", "TH");
    Calendar cal = Calendar.getInstance(loc);
    String calType = cal.getType();
    // Android patch: Force default Gregorian calendar.
    if ( !calType.equals("gregorian")) {
        errln("FAIL: Calendar type for en_TH should still be gregorian");
    }
    // Android patch end.
}
 
Example 5
Source File: IslamicTest.java    From j2objc with Apache License 2.0 4 votes vote down vote up
private void verifyType(Calendar c, String expectType) {
    String theType = c.getType();
    if(!theType.equals(expectType)) {
        errln("Expected calendar to be type " + expectType + " but instead it is " + theType);
    }
}
 
Example 6
Source File: GlobalizationPreferencesTest.java    From j2objc with Apache License 2.0 4 votes vote down vote up
@Test
public void TestCalendar() {
    GlobalizationPreferences gp = new GlobalizationPreferences();

    // Set locale - pt_BR
    logln("Set locale - pt");
    gp.setLocale(new ULocale("pt"));
    Calendar cal = gp.getCalendar();
    String calType = cal.getType();
    if (!calType.equals("gregorian")) {
        errln("FAIL: Calendar type is " + calType + " Expected: gregorian");
    }

    // Set a list of locales
    logln("Set locales - en, en_JP, en_GB");
    ULocale[] locales = new ULocale[3];
    locales[0] = new ULocale("en");
    locales[1] = new ULocale("en_JP");
    locales[2] = new ULocale("en_GB");
    gp.setLocales(locales);

    cal = gp.getCalendar();
    ULocale calLocale = cal.getLocale(ULocale.VALID_LOCALE);
    if (!calLocale.equals(locales[2])) {
        errln("FAIL: Calendar locale is " + calLocale.toString() + " - Expected: en_GB");
    }

    // Set ecplicit calendar
    logln("Set Japanese calendar to this object");
    JapaneseCalendar jcal = new JapaneseCalendar();
    gp.setCalendar(jcal);
    cal = gp.getCalendar();
    calType = cal.getType();
    if (!calType.equals("japanese")) {
        errln("FAIL: Calendar type is " + calType + " Expected: japanese");
    }

    jcal.setFirstDayOfWeek(3);
    if (cal.getFirstDayOfWeek() == jcal.getFirstDayOfWeek()) {
        errln("FAIL: Calendar returned by getCalendar must be a safe copy");
    }
    cal.setFirstDayOfWeek(3);
    Calendar cal1 = gp.getCalendar();
    if (cal1.getFirstDayOfWeek() == cal.getFirstDayOfWeek()) {
        errln("FAIL: Calendar returned by getCalendar must be a safe copy");
    }

    // Freeze
    logln("Freeze this object");
    IslamicCalendar ical = new IslamicCalendar();
    boolean bFrozen = false;
    gp.freeze();
    try {
        gp.setCalendar(ical);
    } catch (UnsupportedOperationException uoe) {
        logln("setCalendar is blocked");
        bFrozen = true;
    }
    if (!bFrozen) {
        errln("FAIL: setCalendar must be blocked");
    }

    // Safe clone
    logln("cloneAsThawed");
    GlobalizationPreferences gp1 = (GlobalizationPreferences)gp.cloneAsThawed();
    cal = gp.getCalendar();
    calType = cal.getType();
    if (!calType.equals("japanese")) {
        errln("FAIL: Calendar type afte clone is " + calType + " Expected: japanese");
    }

    logln("Set islamic calendar");
    gp1.setCalendar(ical);
    cal = gp1.getCalendar();
    calType = cal.getType();
    if (!calType.equals("islamic-civil")) { // default constructed IslamicCalendar is islamic-civil
        errln("FAIL: Calendar type afte clone is " + calType + " Expected: islamic-civil");
    }
}