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

The following examples show how to use android.icu.util.TimeZone#setDefault() . 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: DateFormatRegressionTest.java    From j2objc with Apache License 2.0 6 votes vote down vote up
/**
 * @bug 4089106
 */
@Test
public void Test4089106() {
    TimeZone def = TimeZone.getDefault();
    try {
        TimeZone z = new SimpleTimeZone((int) (1.25 * 3600000), "FAKEZONE");
        TimeZone.setDefault(z);
        // Android patch (http://b/28949992) start.
        // ICU TimeZone.setDefault() not supported on Android.
        z = TimeZone.getDefault();
        // Android patch (http://b/28949992) end.
        SimpleDateFormat f = new SimpleDateFormat();
        if (!f.getTimeZone().equals(z))
            errln("Fail: SimpleTimeZone should use TimeZone.getDefault()");
    } finally {
        TimeZone.setDefault(def);
    }
}
 
Example 2
Source File: TestFmwk.java    From j2objc with Apache License 2.0 5 votes vote down vote up
@Before
public void testInitialize() {
    Locale.setDefault(defaultLocale);
    TimeZone.setDefault(defaultTimeZone);

    /* J2ObjC removed
    if (getParams().testSecurityManager != null) {
        System.setSecurityManager(getParams().testSecurityManager);
    } */
}
 
Example 3
Source File: CalendarRegressionTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
@Test
public void Test4083167() {
    TimeZone saveZone = TimeZone.getDefault();
    try {
        TimeZone.setDefault(TimeZone.getTimeZone("UTC"));
        Date firstDate = new Date();
        Calendar cal = new GregorianCalendar();
        cal.setTime(firstDate);
        long firstMillisInDay = cal.get(Calendar.HOUR_OF_DAY) * 3600000L +
            cal.get(Calendar.MINUTE) * 60000L +
            cal.get(Calendar.SECOND) * 1000L +
            cal.get(Calendar.MILLISECOND);
        
        logln("Current time: " + firstDate.toString());

        for (int validity=0; validity<30; validity++) {
            Date lastDate = new Date(firstDate.getTime() +
                                     (long)validity*1000*24*60*60);
            cal.setTime(lastDate);
            long millisInDay = cal.get(Calendar.HOUR_OF_DAY) * 3600000L +
                cal.get(Calendar.MINUTE) * 60000L +
                cal.get(Calendar.SECOND) * 1000L +
                cal.get(Calendar.MILLISECOND);
            if (firstMillisInDay != millisInDay) 
                errln("Day has shifted " + lastDate);
        }
    }
    finally {
        TimeZone.setDefault(saveZone);
    }
}
 
Example 4
Source File: CalendarRegressionTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * Calendar and Date HOUR broken. If HOUR is out-of-range, Calendar and Date
 * classes will misbehave.
 */
@Test
public void Test4162587() {
    TimeZone tz = TimeZone.getTimeZone("PST");
    TimeZone.setDefault(tz);
    GregorianCalendar cal = new GregorianCalendar(tz);
    Date d;
    
    for (int i=0; i<5; ++i) {
        if (i>0) logln("---");

        cal.clear();
        cal.set(1998, Calendar.APRIL, 5, i, 0);
        d = cal.getTime();
        String s0 = d.toString();
        logln("0 " + i + ": " + s0);

        cal.clear();
        cal.set(1998, Calendar.APRIL, 4, i+24, 0);
        d = cal.getTime();
        String sPlus = d.toString();
        logln("+ " + i + ": " + sPlus);

        cal.clear();
        cal.set(1998, Calendar.APRIL, 6, i-24, 0);
        d = cal.getTime();
        String sMinus = d.toString();
        logln("- " + i + ": " + sMinus);

        if (!s0.equals(sPlus) || !s0.equals(sMinus)) {
            errln("Fail: All three lines must match");
        }
    }
}
 
Example 5
Source File: TimeZoneRegressionTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * CANNOT REPRODUDE
 *
 * Yet another _alleged_ bug in TimeZone.getOffset(), a method that never
 * should have been made public.  It's simply too hard to use correctly.
 *
 * The original test code failed to do the following:
 * (1) Call Calendar.setTime() before getting the fields!
 * (2) Use the right millis (as usual) for getOffset(); they were passing
 *     in the MILLIS field, instead of the STANDARD MILLIS IN DAY.
 * When you fix these two problems, the test passes, as expected.
 */
@Test
public void Test4126678() {
// Note: this test depends on the PST time zone.
TimeZone initialZone = TimeZone.getDefault();
    Calendar cal = Calendar.getInstance();
    TimeZone tz = TimeZone.getTimeZone("PST");
TimeZone.setDefault(tz);
    cal.setTimeZone(tz);

    java.util.Calendar tempcal = java.util.Calendar.getInstance();
    tempcal.clear();
    tempcal.set(1998, Calendar.APRIL, 5, 10, 0);
    Date dt = tempcal.getTime();
// the dt value is local time in PST.
    if (!tz.inDaylightTime(dt))
        errln("We're not in Daylight Savings Time and we should be.\n");

    cal.setTime(dt);
    int era = cal.get(Calendar.ERA);
    int year = cal.get(Calendar.YEAR);
    int month = cal.get(Calendar.MONTH);
    int day = cal.get(Calendar.DATE);
    int dayOfWeek = cal.get(Calendar.DAY_OF_WEEK);
    int millis = cal.get(Calendar.MILLISECOND) +
        (cal.get(Calendar.SECOND) +
         (cal.get(Calendar.MINUTE) +
          (cal.get(Calendar.HOUR) * 60) * 60) * 1000) -
        cal.get(Calendar.DST_OFFSET);

    long offset = tz.getOffset(era, year, month, day, dayOfWeek, millis);
    long raw_offset = tz.getRawOffset();
    if (offset == raw_offset)
        errln("Offsets should not match when in DST");

// restore the initial time zone so that this test case
// doesn't affect the others.
TimeZone.setDefault(initialZone);
}
 
Example 6
Source File: DateFormatRegressionTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * @bug 4103341
 */
@Test
public void Test4103341() {
    TimeZone saveZone = TimeZone.getDefault();
    try {
        // {sfb} changed from adoptDefault to setDefault
        TimeZone.setDefault(TimeZone.getTimeZone("CST"));
        SimpleDateFormat simple = new SimpleDateFormat("MM/dd/yyyy HH:mm");
        TimeZone temp = TimeZone.getDefault();
        if (!simple.getTimeZone().equals(temp))
            errln("Fail: SimpleDateFormat not using default zone");
    } finally {
        TimeZone.setDefault(saveZone);
    }
}
 
Example 7
Source File: CalendarRegressionTest.java    From j2objc with Apache License 2.0 4 votes vote down vote up
/**
 * DateFormat class mistakes date style and time style as follows: -
 * DateFormat.getDateTimeInstance takes date style as time style, and time
 * style as date style - If a Calendar is passed to
 * DateFormat.getDateInstance, it returns time instance - If a Calendar is
 * passed to DateFormat.getTimeInstance, it returns date instance
 */
@Test
public void TestDateFormatFactoryJ26() {
    TimeZone zone = TimeZone.getDefault();
    try {
        Locale loc = Locale.US;
        TimeZone.setDefault(TimeZone.getTimeZone("America/Los_Angeles"));
        java.util.Calendar tempcal = java.util.Calendar.getInstance();
        tempcal.set(2001, Calendar.APRIL, 5, 17, 43, 53);
        Date date = tempcal.getTime();
        Calendar cal = Calendar.getInstance(loc);
        Object[] DATA = {
            DateFormat.getDateInstance(DateFormat.SHORT, loc),
            "DateFormat.getDateInstance(DateFormat.SHORT, loc)",
            "4/5/01",

            DateFormat.getTimeInstance(DateFormat.SHORT, loc),
            "DateFormat.getTimeInstance(DateFormat.SHORT, loc)",
            "5:43 PM",

            DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.SHORT, loc),
            "DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.SHORT, loc)",
            "Thursday, April 5, 2001 at 5:43 PM",

            DateFormat.getDateInstance(cal, DateFormat.SHORT, loc),
            "DateFormat.getDateInstance(cal, DateFormat.SHORT, loc)",
            "4/5/01",

            DateFormat.getTimeInstance(cal, DateFormat.SHORT, loc),
            "DateFormat.getTimeInstance(cal, DateFormat.SHORT, loc)",
            "5:43 PM",

            DateFormat.getDateTimeInstance(cal, DateFormat.FULL, DateFormat.SHORT, loc),
            "DateFormat.getDateTimeInstance(cal, DateFormat.FULL, DateFormat.SHORT, loc)",
            "Thursday, April 5, 2001 at 5:43 PM",
        
            cal.getDateTimeFormat(DateFormat.SHORT, DateFormat.FULL, loc),
            "cal.getDateTimeFormat(DateFormat.SHORT, DateFormat.FULL, loc)",
            "4/5/01, 5:43:53 PM Pacific Daylight Time",

            cal.getDateTimeFormat(DateFormat.FULL, DateFormat.SHORT, loc),
            "cal.getDateTimeFormat(DateFormat.FULL, DateFormat.SHORT, loc)",
            "Thursday, April 5, 2001 at 5:43 PM",
        };
        for (int i=0; i<DATA.length; i+=3) {
            DateFormat df = (DateFormat) DATA[i];
            String desc = (String) DATA[i+1];
            String exp = (String) DATA[i+2];
            String got = df.format(date);
            if (got.equals(exp)) {
                logln("Ok: " + desc + " => " + got);
            } else {
                errln("FAIL: " + desc + " => " + got + ", expected " + exp);
            }
        }
    } finally {
        TimeZone.setDefault(zone);
    }
}
 
Example 8
Source File: TimeZoneTest.java    From j2objc with Apache License 2.0 4 votes vote down vote up
@Test
public void TestSetDefault() {
    java.util.TimeZone save = java.util.TimeZone.getDefault();

    /*
     * America/Caracs (Venezuela) changed the base offset from -4:00 to
     * -4:30 on Dec 9, 2007.
     */

    TimeZone icuCaracas = TimeZone.getTimeZone("America/Caracas", TimeZone.TIMEZONE_ICU);
    java.util.TimeZone jdkCaracas = java.util.TimeZone.getTimeZone("America/Caracas");

    // Set JDK America/Caracas as the default
    java.util.TimeZone.setDefault(jdkCaracas);

    java.util.Calendar jdkCal = java.util.Calendar.getInstance();
    jdkCal.clear();
    jdkCal.set(2007, java.util.Calendar.JANUARY, 1);

    int rawOffset = jdkCal.get(java.util.Calendar.ZONE_OFFSET);
    int dstSavings = jdkCal.get(java.util.Calendar.DST_OFFSET);

    int[] offsets = new int[2];
    icuCaracas.getOffset(jdkCal.getTime().getTime()/*jdkCal.getTimeInMillis()*/, false, offsets);

    boolean isTimeZoneSynchronized = true;

    if (rawOffset != offsets[0] || dstSavings != offsets[1]) {
        // JDK time zone rule is out of sync...
        logln("Rule for JDK America/Caracas is not same with ICU.  Skipping the rest.");
        isTimeZoneSynchronized = false;
    }

    if (isTimeZoneSynchronized) {
        // If JDK America/Caracas uses the same rule with ICU,
        // the following code should work well.
        TimeZone.setDefault(icuCaracas);

        // Create a new JDK calendar instance again.
        // This calendar should reflect the new default
        // set by ICU TimeZone#setDefault.
        jdkCal = java.util.Calendar.getInstance();
        jdkCal.clear();
        jdkCal.set(2007, java.util.Calendar.JANUARY, 1);

        rawOffset = jdkCal.get(java.util.Calendar.ZONE_OFFSET);
        dstSavings = jdkCal.get(java.util.Calendar.DST_OFFSET);

        if (rawOffset != offsets[0] || dstSavings != offsets[1]) {
            errln("ERROR: Got offset [raw:" + rawOffset + "/dst:" + dstSavings
                      + "] Expected [raw:" + offsets[0] + "/dst:" + offsets[1] + "]");
        }
    }

    // Restore the original JDK time zone
    java.util.TimeZone.setDefault(save);
}
 
Example 9
Source File: TimeZoneRegressionTest.java    From j2objc with Apache License 2.0 4 votes vote down vote up
/**
 * TimeZone broken at midnight.  The TimeZone code fails to handle
 * transitions at midnight correctly.
 */
@Test
public void Test4162593() {
    SimpleDateFormat fmt = new SimpleDateFormat("z", Locale.US);
    final int ONE_HOUR = 60*60*1000;
    final float H = (float) ONE_HOUR;
    TimeZone initialZone = TimeZone.getDefault();
    SimpleDateFormat sdf = new SimpleDateFormat("MMM dd yyyy HH:mm z");

    SimpleTimeZone asuncion = new SimpleTimeZone(-4*ONE_HOUR, "America/Asuncion" /*PY%sT*/,
        Calendar.OCTOBER, 1, 0 /*DOM*/, 0*ONE_HOUR,
        Calendar.MARCH, 1, 0 /*DOM*/, 0*ONE_HOUR, 1*ONE_HOUR);

    /* Zone
     * Starting time
     * Transition expected between start+1H and start+2H
     */
    Object[] DATA = {
        new SimpleTimeZone(2*ONE_HOUR, "Asia/Damascus" /*EE%sT*/,
            Calendar.APRIL, 1, 0 /*DOM*/, 0*ONE_HOUR,
            Calendar.OCTOBER, 1, 0 /*DOM*/, 0*ONE_HOUR, 1*ONE_HOUR),
        new int[] {1998, Calendar.SEPTEMBER, 30, 22, 0},
        Boolean.TRUE,

        asuncion,
        new int[] {2000, Calendar.FEBRUARY, 28, 22, 0},
        Boolean.FALSE,

        asuncion,
        new int[] {2000, Calendar.FEBRUARY, 29, 22, 0},
        Boolean.TRUE,
    };

    String[] zone = new String[4];

    for (int j=0; j<DATA.length; j+=3) {
        TimeZone tz = (TimeZone)DATA[j];
        TimeZone.setDefault(tz);
        fmt.setTimeZone(tz);
        sdf.setTimeZone(tz);

        // Must construct the Date object AFTER setting the default zone
        int[] p = (int[])DATA[j+1];
        Calendar cal = Calendar.getInstance();
        cal.clear();
        cal.set(p[0], p[1], p[2], p[3], p[4]);
        long start = cal.getTime().getTime();
        boolean transitionExpected = ((Boolean)DATA[j+2]).booleanValue();

        logln(tz.getID() + ":");
        for (int i=0; i<4; ++i) {
            Date d = new Date(start + i*ONE_HOUR);
            zone[i] = fmt.format(d);
            logln("" + i + ": " + sdf.format(d) + " => " + zone[i] +
                  " (" + d.getTime()/H + ")");
        }
        cal.set(p[0], p[1], p[2], 0, 0);
        for (int i=0; i<4; ++i) {
            int h = 22+i;
            int dom = p[2]+(h>=24?1:0);
            h %= 24;
            int ms = h*ONE_HOUR;
            cal.clear();
            cal.set(p[0], p[1], dom, 0, 0);
            int off = tz.getOffset(GregorianCalendar.AD,
                                   cal.get(Calendar.YEAR),
                                   cal.get(Calendar.MONTH),
                                   cal.get(Calendar.DATE),
                                   cal.get(Calendar.DAY_OF_WEEK),
                                   ms);
            cal.add(Calendar.HOUR, h);
            int dstOffset = cal.get(Calendar.DST_OFFSET);
            logln("h=" + h + "; dom=" + dom +
                  "; ZONE_OFFSET=" + cal.get(Calendar.ZONE_OFFSET)/H +
                  "; DST_OFFSET=" + dstOffset/H +
                  "; getOffset()=" + off/H +
                  " (" + cal.getTime().getTime()/H + ")");
        }
        if (zone[0].equals(zone[1]) &&
            (zone[1].equals(zone[2]) != transitionExpected) &&
            zone[2].equals(zone[3])) {
            logln("Ok: transition " + transitionExpected);
        } else {
            errln("FAIL: expected " +
                  (transitionExpected?"transition":"no transition"));
        }
    }

// restore the initial time zone so that this test case
// doesn't affect the others.
TimeZone.setDefault(initialZone);
}
 
Example 10
Source File: DateFormatRegressionTest.java    From j2objc with Apache License 2.0 4 votes vote down vote up
/**
 * @bug 4065240
 */
@Test
public void Test4065240() {
    Date curDate;
    DateFormat shortdate, fulldate;
    String strShortDate, strFullDate;
    Locale saveLocale = Locale.getDefault();
    TimeZone saveZone = TimeZone.getDefault();

    try {
        Locale curLocale = new Locale("de", "DE");
        Locale.setDefault(curLocale);
        // {sfb} adoptDefault instead of setDefault
        //TimeZone.setDefault(TimeZone.createTimeZone("EST"));
        TimeZone.setDefault(TimeZone.getTimeZone("EST"));
        Calendar cal = Calendar.getInstance();
        cal.clear();
        cal.set(98 + 1900, 0, 1);
        curDate = cal.getTime();
        shortdate = DateFormat.getDateInstance(DateFormat.SHORT);
        fulldate = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG);
        strShortDate = "The current date (short form) is ";
        String temp;
        temp = shortdate.format(curDate);
        strShortDate += temp;
        strFullDate = "The current date (long form) is ";
        String temp2 = fulldate.format(curDate);
        strFullDate += temp2;

        logln(strShortDate);
        logln(strFullDate);

        // {sfb} What to do with resource bundle stuff?????

        // Check to see if the resource is present; if not, we can't test
        //ResourceBundle bundle = //The variable is never used
        //    ICULocaleData.getBundle("DateFormatZoneData", curLocale); 

        // {sfb} API change to ResourceBundle -- add getLocale()
        /*if (bundle.getLocale().getLanguage().equals("de")) {
            // UPDATE THIS AS ZONE NAME RESOURCE FOR <EST> in de_DE is updated
            if (!strFullDate.endsWith("GMT-05:00"))
                errln("Fail: Want GMT-05:00");
        } else {
            logln("*** TEST COULD NOT BE COMPLETED BECAUSE DateFormatZoneData ***");
            logln("*** FOR LOCALE de OR de_DE IS MISSING ***");
        }*/
    } catch (Exception e) {
        logln(e.getMessage());
    } finally {
        Locale.setDefault(saveLocale);
        TimeZone.setDefault(saveZone);
    }

}