Java Code Examples for android.icu.util.TimeZone#getDefaultTimeZoneType()

The following examples show how to use android.icu.util.TimeZone#getDefaultTimeZoneType() . 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: JapaneseTest.java    From j2objc with Apache License 2.0 4 votes vote down vote up
@Test
public void Test5345parse() {
    // Test parse with incomplete information
    DateFormat fmt2= DateFormat.getDateInstance(); //DateFormat.LONG, Locale.US);
    JapaneseCalendar c = new JapaneseCalendar(TimeZone.getDefault(), new ULocale("en_US"));
    SimpleDateFormat fmt = (SimpleDateFormat)c.getDateTimeFormat(1,1,new ULocale("en_US@calendar=japanese"));
    fmt.applyPattern("G y");
    logln("fmt's locale = " + fmt.getLocale(ULocale.ACTUAL_LOCALE));
    //SimpleDateFormat fmt = new SimpleDateFormat("G y", new Locale("en_US@calendar=japanese"));
    long aDateLong = -3197117222000L; // 1868-09-08 00:00 Pacific Time (GMT-07:52:58)
    if (TimeZone.getDefaultTimeZoneType() == TimeZone.TIMEZONE_JDK) {
        // Java time zone implementation does not support LMTs
        aDateLong = -3197116800000L; // 1868-09-08 00:00 Pacific Time (GMT-08:00)
    }
    Date aDate = new Date(aDateLong);
    logln("aDate: " + aDate.toString() +", from " + aDateLong);
    String str;
    str = fmt2.format(aDate);
    logln("Test Date: " + str);
    str = fmt.format(aDate);
    logln("as Japanese Calendar: " + str);
    String expected = "Meiji 1";
    if(!str.equals(expected)) {
        errln("FAIL: Expected " + expected + " but got " + str);
    }
    Date otherDate;
    try {
        otherDate = fmt.parse(expected);
        if(!otherDate.equals(aDate)) { 
            String str3;
//            ParsePosition pp;
            Date dd = fmt.parse(expected);
            str3 = fmt.format(otherDate);
            long oLong = otherDate.getTime();
            long aLong = otherDate.getTime();
            
            errln("FAIL: Parse incorrect of " + expected + ":  wanted " + aDate + " ("+aLong+"), but got " +  " " +
                otherDate + " ("+oLong+") = " + str3 + " not " + dd.toString() );


        } else {
            logln("Parsed OK: " + expected);
        }
    } catch(java.text.ParseException pe) {
        errln("FAIL: ParseException: " + pe.toString());
        pe.printStackTrace();
    }
}
 
Example 2
Source File: TimeZoneTest.java    From j2objc with Apache License 2.0 4 votes vote down vote up
/**
 * As part of the VM fix (see CCC approved RFE 4028006, bug
 * 4044013), TimeZone.getTimeZone() has been modified to recognize
 * generic IDs of the form GMT[+-]hh:mm, GMT[+-]hhmm, and
 * GMT[+-]hh.  Test this behavior here.
 *
 * Bug 4044013
 */
@Test
public void TestCustomParse() {
    String[] DATA = {
        // ID               offset(sec)     output ID
        "GMT",              "0",            "GMT",      // system ID
        "GMT-YOUR.AD.HERE", "0",            TimeZone.UNKNOWN_ZONE_ID,
        "GMT0",             "0",            "GMT0",     // system ID
        "GMT+0",            "0",            "GMT+0",    // system ID
        "GMT+1",            "3600",         "GMT+01:00",
        "GMT-0030",         "-1800",        "GMT-00:30",
        "GMT+15:99",        "0",            TimeZone.UNKNOWN_ZONE_ID,
        "GMT+",             "0",            TimeZone.UNKNOWN_ZONE_ID,
        "GMT-",             "0",            TimeZone.UNKNOWN_ZONE_ID,
        "GMT+0:",           "0",            TimeZone.UNKNOWN_ZONE_ID,
        "GMT-:",            "0",            TimeZone.UNKNOWN_ZONE_ID,
        "GMT+0010",         "600",          "GMT+00:10",
        "GMT-10",           "-36000",       "GMT-10:00",
        "GMT+30",           "0",            TimeZone.UNKNOWN_ZONE_ID,
        "GMT-3:30",         "-12600",       "GMT-03:30",
        "GMT-230",          "-9000",        "GMT-02:30",
        "GMT+05:13:05",     "18785",        "GMT+05:13:05",
        "GMT-71023",        "-25823",       "GMT-07:10:23",
        "GMT+01:23:45:67",  "0",            TimeZone.UNKNOWN_ZONE_ID,
        "GMT+01:234",       "0",            TimeZone.UNKNOWN_ZONE_ID,
        "GMT-2:31:123",     "0",            TimeZone.UNKNOWN_ZONE_ID,
        "GMT+3:75",         "0",            TimeZone.UNKNOWN_ZONE_ID,
        "GMT-01010101",     "0",            TimeZone.UNKNOWN_ZONE_ID,
    };
    for (int i = 0; i < DATA.length; i += 3) {
        String id = DATA[i];
        int offset = Integer.parseInt(DATA[i+1]);
        String expId = DATA[i+2];

        TimeZone zone = TimeZone.getTimeZone(id);
        String gotID = zone.getID();
        int gotOffset = zone.getRawOffset()/1000;

        logln(id + " -> " + gotID + " " + gotOffset);

        if (offset != gotOffset) {
            errln("FAIL: Unexpected offset for " + id + " - returned:" + gotOffset + " expected:" + offset);
        }
        if (!expId.equals(gotID)) {
            if (TimeZone.getDefaultTimeZoneType() != TimeZone.TIMEZONE_ICU) {
                logln("ID for " + id + " - returned:" + gotID + " expected:" + expId);
            } else {
                errln("FAIL: Unexpected ID for " + id + " - returned:" + gotID + " expected:" + expId);
            }
        }
    }
}
 
Example 3
Source File: TimeZoneTest.java    From j2objc with Apache License 2.0 4 votes vote down vote up
@Test
public void TestDisplayNamesMeta() {
    final Integer TZSHORT = new Integer(TimeZone.SHORT);
    final Integer TZLONG = new Integer(TimeZone.LONG);

    final Object[][] zoneDisplayTestData = {
        //  zone id             locale  summer          format      expected display name
        {"Europe/London",       "en",   Boolean.FALSE,  TZSHORT,    "GMT"},
        {"Europe/London",       "en",   Boolean.FALSE,  TZLONG,     "Greenwich Mean Time"},
        {"Europe/London",       "en",   Boolean.TRUE,   TZSHORT,    "GMT+1" /*"BST"*/},
        {"Europe/London",       "en",   Boolean.TRUE,   TZLONG,     "British Summer Time"},

        {"America/Anchorage",   "en",   Boolean.FALSE,  TZSHORT,    "AKST"},
        {"America/Anchorage",   "en",   Boolean.FALSE,  TZLONG,     "Alaska Standard Time"},
        {"America/Anchorage",   "en",   Boolean.TRUE,   TZSHORT,    "AKDT"},
        {"America/Anchorage",   "en",   Boolean.TRUE,   TZLONG,     "Alaska Daylight Time"},

        // Southern Hemisphere, all data from meta:Australia_Western
        {"Australia/Perth",     "en",   Boolean.FALSE,  TZSHORT,    "GMT+8"/*"AWST"*/},
        {"Australia/Perth",     "en",   Boolean.FALSE,  TZLONG,     "Australian Western Standard Time"},
        // Note: Perth does not observe DST currently. When display name is missing,
        // the localized GMT format with the current offset is used even daylight name was
        // requested. See #9350.
        {"Australia/Perth",     "en",   Boolean.TRUE,   TZSHORT,    "GMT+8"/*"AWDT"*/},
        {"Australia/Perth",     "en",   Boolean.TRUE,   TZLONG,     "Australian Western Daylight Time"},

        {"America/Sao_Paulo",   "en",   Boolean.FALSE,  TZSHORT,    "GMT-3"/*"BRT"*/},
        {"America/Sao_Paulo",   "en",   Boolean.FALSE,  TZLONG,     "Brasilia Standard Time"},
        {"America/Sao_Paulo",   "en",   Boolean.TRUE,   TZSHORT,    "GMT-2"/*"BRST"*/},
        {"America/Sao_Paulo",   "en",   Boolean.TRUE,   TZLONG,     "Brasilia Summer Time"},

        // No Summer Time, but had it before 1983.
        {"Pacific/Honolulu",    "en",   Boolean.FALSE,  TZSHORT,    "HST"},
        {"Pacific/Honolulu",    "en",   Boolean.FALSE,  TZLONG,     "Hawaii-Aleutian Standard Time"},
        {"Pacific/Honolulu",    "en",   Boolean.TRUE,   TZSHORT,    "HDT"},
        {"Pacific/Honolulu",    "en",   Boolean.TRUE,   TZLONG,     "Hawaii-Aleutian Daylight Time"},

        // Northern, has Summer, not commonly used.
        {"Europe/Helsinki",     "en",   Boolean.FALSE,  TZSHORT,    "GMT+2"/*"EET"*/},
        {"Europe/Helsinki",     "en",   Boolean.FALSE,  TZLONG,     "Eastern European Standard Time"},
        {"Europe/Helsinki",     "en",   Boolean.TRUE,   TZSHORT,    "GMT+3"/*"EEST"*/},
        {"Europe/Helsinki",     "en",   Boolean.TRUE,   TZLONG,     "Eastern European Summer Time"},

        // Repeating the test data for DST.  The test data below trigger the problem reported
        // by Ticket#6644
        {"Europe/London",       "en",   Boolean.TRUE,   TZSHORT,    "GMT+1" /*"BST"*/},
        {"Europe/London",       "en",   Boolean.TRUE,   TZLONG,     "British Summer Time"},
    };

    boolean isICUTimeZone = (TimeZone.getDefaultTimeZoneType() == TimeZone.TIMEZONE_ICU);

    boolean sawAnError = false;
    for (int testNum = 0; testNum < zoneDisplayTestData.length; testNum++) {
        ULocale locale = new ULocale((String)zoneDisplayTestData[testNum][1]);
        TimeZone zone = TimeZone.getTimeZone((String)zoneDisplayTestData[testNum][0]);
        String displayName = zone.getDisplayName(((Boolean)zoneDisplayTestData[testNum][2]).booleanValue(),
                ((Integer)zoneDisplayTestData[testNum][3]).intValue());
        if (!displayName.equals(zoneDisplayTestData[testNum][4])) {
            if (isDevelopmentBuild
                    && (isICUTimeZone || !((Boolean)zoneDisplayTestData[testNum][2]).booleanValue())) {
                sawAnError = true;
                errln("Incorrect time zone display name.  zone = "
                        + zoneDisplayTestData[testNum][0] + ",\n"
                        + "   locale = " + locale
                        + ",   style = " + (zoneDisplayTestData[testNum][3] == TZSHORT ? "SHORT" : "LONG")
                        + ",   Summertime = " + zoneDisplayTestData[testNum][2] + "\n"
                        + "   Expected " + zoneDisplayTestData[testNum][4]
                        + ",   Got " + displayName);
            } else {
                logln("Incorrect time zone display name.  zone = "
                        + zoneDisplayTestData[testNum][0] + ",\n"
                        + "   locale = " + locale
                        + ",   style = " + (zoneDisplayTestData[testNum][3] == TZSHORT ? "SHORT" : "LONG")
                        + ",   Summertime = " + zoneDisplayTestData[testNum][2] + "\n"
                        + "   Expected " + zoneDisplayTestData[testNum][4]
                        + ",   Got " + displayName);
            }
        }
    }
    if (sawAnError) {
        logln("Note: Errors could be the result of changes to zoneStrings locale data");
    }
}
 
Example 4
Source File: TimeZoneRegressionTest.java    From j2objc with Apache License 2.0 4 votes vote down vote up
/**
 * Test setRawOffset works OK with system timezone
 */
@Test
public void TestT5280() {
    boolean isJdkZone = (TimeZone.getDefaultTimeZoneType() == TimeZone.TIMEZONE_JDK);
    String[] tzids = TimeZone.getAvailableIDs();
    for (int i = 0; i < tzids.length; i++) {
        TimeZone tz = TimeZone.getTimeZone(tzids[i]);
        boolean useDst = tz.useDaylightTime();

        // Increase offset for 30 minutes
        int newRawOffset = tz.getRawOffset() + 30*60*1000;
        try {
            tz.setRawOffset(newRawOffset);
        } catch (Exception e) {
            errln("FAIL: setRawOffset throws an exception");
        }
        int offset = tz.getRawOffset();
        if (offset != newRawOffset) {
            if (isJdkZone) {
                // JDK TimeZone#setRawOffset() only update the last rule, and getRawOffset() returns
                // the current raw offset. Therefore, they might be different.
                logln("Modified zone(" + tz.getID() + ") - getRawOffset returns " + offset + "/ Expected: " + newRawOffset);
            } else {
                errln("FAIL: Modified zone(" + tz.getID() + ") - getRawOffset returns " + offset + "/ Expected: " + newRawOffset);
            }
        }
        // Ticket#5917
        // Check if DST observation status is not unexpectedly changed.
        boolean newDst = tz.useDaylightTime();
        if (useDst != newDst) {
            errln("FAIL: Modified zone(" + tz.getID() + ") - useDaylightTime has changed from " + useDst + " to " + newDst);
        }
        // Make sure the offset is preserved in a clone
        TimeZone tzClone = (TimeZone)tz.clone();
        int offsetC = tzClone.getRawOffset();
        if (offsetC != newRawOffset) {
            if (isJdkZone) {
                logln("Cloned modified zone(" + tz.getID() + ") - getRawOffset returns " + offsetC + "/ Expected: " + newRawOffset);
            } else {
                errln("FAIL: Cloned modified zone(" + tz.getID() + ") - getRawOffset returns " + offsetC + "/ Expected: " + newRawOffset);
            }
        }

        if (offset != offsetC) {
            errln("FAIL: Different raw offset - Original:" + offset + ", Cloned:" + offsetC);
        }
    }
}