Java Code Examples for android.icu.text.DateFormat#setTimeZone()

The following examples show how to use android.icu.text.DateFormat#setTimeZone() . 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: TimeZoneBoundaryTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
private static String showDate(Date d, TimeZone zone)
{
    DateFormat fmt = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG);
    fmt.setTimeZone(zone);
    java.util.Calendar cal = java.util.Calendar.getInstance();
    cal.setTime(d);
    return "" + (cal.get(Calendar.YEAR) - 1900) + "/" + 
           showNN(cal.get(Calendar.MONTH) + 1) + "/" + 
           showNN(cal.get(Calendar.DAY_OF_MONTH)) + " " + 
           showNN(cal.get(Calendar.HOUR_OF_DAY)) + ":" + 
           showNN(cal.get(Calendar.MINUTE)) + " \"" + d + "\" = " +
           fmt.format(d) + " = " + d.getTime();
}
 
Example 2
Source File: CalendarTestFmwk.java    From j2objc with Apache License 2.0 4 votes vote down vote up
/**
 * Iterates through a list of calendar <code>TestCase</code> objects and
 * makes sure that the time-to-fields and fields-to-time calculations work
 * correnctly for the values in each test case.
 */
protected void doTestCases(TestCase[] cases, Calendar cal)
{
    cal.setTimeZone(UTC);
    
    // Get a format to use for printing dates in the calendar system we're testing
    DateFormat format = DateFormat.getDateTimeInstance(cal, DateFormat.SHORT, -1, Locale.getDefault());

    final String pattern = (cal instanceof ChineseCalendar) ?
        "E MMl/dd/y G HH:mm:ss.S z" :
        "E, MM/dd/yyyy G HH:mm:ss.S z";

    ((SimpleDateFormat)format).applyPattern(pattern);

    // This format is used for printing Gregorian dates.
    DateFormat gregFormat = new SimpleDateFormat(pattern);
    gregFormat.setTimeZone(UTC);

    GregorianCalendar pureGreg = new GregorianCalendar(UTC);
    pureGreg.setGregorianChange(new Date(Long.MIN_VALUE));
    DateFormat pureGregFmt = new SimpleDateFormat("E M/d/yyyy G");
    pureGregFmt.setCalendar(pureGreg);
    
    // Now iterate through the test cases and see what happens
    for (int i = 0; i < cases.length; i++)
    {
        logln("\ntest case: " + i);
        TestCase test = cases[i];
        
        //
        // First we want to make sure that the millis -> fields calculation works
        // test.applyTime will call setTime() on the calendar object, and
        // test.fieldsEqual will retrieve all of the field values and make sure
        // that they're the same as the ones in the testcase
        //
        test.applyTime(cal);
        if (!test.fieldsEqual(cal, this)) {
            errln("Fail: (millis=>fields) " +
                  gregFormat.format(test.getTime()) + " => " +
                  format.format(cal.getTime()) +
                  ", expected " + test);
        }

        //
        // If that was OK, check the fields -> millis calculation
        // test.applyFields will set all of the calendar's fields to 
        // match those in the test case.
        //
        cal.clear();
        test.applyFields(cal);
        if (!test.equals(cal)) {
            errln("Fail: (fields=>millis) " + test + " => " +
                  pureGregFmt.format(cal.getTime()) +
                  ", expected " + pureGregFmt.format(test.getTime()));
        }
    }
}
 
Example 3
Source File: TimeZoneRegressionTest.java    From j2objc with Apache License 2.0 4 votes vote down vote up
/**
 * getDisplayName doesn't work with unusual savings/offsets.
 */
@Test
public void Test4176686() {
    // Construct a zone that does not observe DST but
    // that does have a DST savings (which should be ignored).
    int offset = 90 * 60000; // 1:30
    SimpleTimeZone z1 = new SimpleTimeZone(offset, "_std_zone_");
    z1.setDSTSavings(45 * 60000); // 0:45

    // Construct a zone that observes DST for the first 6 months.
    SimpleTimeZone z2 = new SimpleTimeZone(offset, "_dst_zone_");
    z2.setDSTSavings(45 * 60000); // 0:45
    z2.setStartRule(Calendar.JANUARY, 1, 0);
    z2.setEndRule(Calendar.JULY, 1, 0);

    // Also check DateFormat
    DateFormat fmt1 = new SimpleDateFormat("z");
    fmt1.setTimeZone(z1); // Format uses standard zone
    DateFormat fmt2 = new SimpleDateFormat("z");
    fmt2.setTimeZone(z2); // Format uses DST zone
    java.util.Calendar tempcal = java.util.Calendar.getInstance();
    tempcal.clear();
    tempcal.set(1970, Calendar.FEBRUARY, 1);
    Date dst = tempcal.getTime(); // Time in DST
    tempcal.set(1970, Calendar.AUGUST, 1);
    Date std = tempcal.getTime(); // Time in standard

    // Description, Result, Expected Result
    String[] DATA = {
        "getDisplayName(false, SHORT)/std zone",
        z1.getDisplayName(false, TimeZone.SHORT), "GMT+1:30",
        "getDisplayName(false, LONG)/std zone",
        z1.getDisplayName(false, TimeZone.LONG ), "GMT+01:30",
        "getDisplayName(true, SHORT)/std zone",
        z1.getDisplayName(true, TimeZone.SHORT), "GMT+1:30",
        "getDisplayName(true, LONG)/std zone",
        z1.getDisplayName(true, TimeZone.LONG ), "GMT+01:30",
        "getDisplayName(false, SHORT)/dst zone",
        z2.getDisplayName(false, TimeZone.SHORT), "GMT+1:30",
        "getDisplayName(false, LONG)/dst zone",
        z2.getDisplayName(false, TimeZone.LONG ), "GMT+01:30",
        "getDisplayName(true, SHORT)/dst zone",
        z2.getDisplayName(true, TimeZone.SHORT), "GMT+2:15",
        "getDisplayName(true, LONG)/dst zone",
        z2.getDisplayName(true, TimeZone.LONG ), "GMT+02:15",
        "DateFormat.format(std)/std zone", fmt1.format(std), "GMT+1:30",
        "DateFormat.format(dst)/std zone", fmt1.format(dst), "GMT+1:30",
        "DateFormat.format(std)/dst zone", fmt2.format(std), "GMT+1:30",
        "DateFormat.format(dst)/dst zone", fmt2.format(dst), "GMT+2:15",
    };

    for (int i=0; i<DATA.length; i+=3) {
        if (!DATA[i+1].equals(DATA[i+2])) {
            errln("FAIL: " + DATA[i] + " -> " + DATA[i+1] + ", exp " + DATA[i+2]);
        }
    }
}
 
Example 4
Source File: TimeZoneBoundaryTest.java    From j2objc with Apache License 2.0 4 votes vote down vote up
void findDaylightBoundaryUsingTimeZone(Date d, boolean startsInDST,
                                       long expectedBoundary, TimeZone tz)
{
    // Given a date with a year start, find the Daylight onset
    // and end.  The given date should be 1/1/xx in some year.

    // Use a binary search, assuming that we have a Standard
    // time at the midpoint.
    long min = d.getTime();
    long max = min + SIX_MONTHS;

    if (tz.inDaylightTime(d) != startsInDST)
    {
        errln("FAIL: " + tz.getID() + " inDaylightTime(" +
              d + ") != " + startsInDST);
        startsInDST = !startsInDST; // Flip over; find the apparent value
    }

    if (tz.inDaylightTime(new Date(max)) == startsInDST)
    {
        errln("FAIL: " + tz.getID() + " inDaylightTime(" +
              (new Date(max)) + ") != " + (!startsInDST));
        return;
    }

    while ((max - min) >  INTERVAL)
    {
        long mid = (min + max) >> 1;
        boolean isIn = tz.inDaylightTime(new Date(mid));
        if (isIn == startsInDST)
        {
            min = mid;
        }
        else
        {
            max = mid;
        }
    }

    logln(tz.getID() + " Before: " + showDate(min, tz));
    logln(tz.getID() + " After:  " + showDate(max, tz));

    long mindelta = expectedBoundary - min;
    // not used long maxdelta = max - expectedBoundary; 
    DateFormat fmt = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG);
    fmt.setTimeZone(tz);
    if (mindelta >= 0 && mindelta <= INTERVAL &&
        mindelta >= 0 && mindelta <= INTERVAL)
        logln("PASS: Expected boundary at " + expectedBoundary + " = " + fmt.format(new Date(expectedBoundary)));
    else
        errln("FAIL: Expected boundary at " + expectedBoundary + " = " + fmt.format(new Date(expectedBoundary)));
}