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

The following examples show how to use android.icu.util.TimeZone#equals() . 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: TimeZoneFormatTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
public void TestParseCoverage() {
    TimeZone expectedTZ = TimeZone.getTimeZone("America/Los_Angeles");
    TimeZoneFormat fmt = TimeZoneFormat.getInstance(ULocale.ENGLISH);

    // Test parse(String)
    try {
        TimeZone tz1 = fmt.parse("America/Los_Angeles");
        if (tz1 == null) {
            errln("Parse failure using parse(String) - expected: " + expectedTZ.getID());
        } else if (!expectedTZ.equals(tz1)) {
            errln("Parsed TimeZone: '" + tz1.getID()  + "' using parse(String) - expected: "
                    + expectedTZ.getID());
        }
    } catch (ParseException e) {
        errln("Parse failure using parse(String) - expected: " + expectedTZ.getID()
                + " exception: " + e.getMessage());
    }

    // Test parse(String, ParsePosition)
    TimeZone tz2 = fmt.parse("++America/Los_Angeles", new ParsePosition(2));
    if (tz2 == null) {
        errln("Parse failure using parse(String, ParsePosition) - expected: "
                + expectedTZ.getID());
    } else if (!expectedTZ.equals(tz2)) {
        errln("Parsed TimeZone: '" + tz2.getID()  + "' using parse(String, ParsePosition) - expected: "
                + expectedTZ.getID());
    }

    // Test parseObject(String, ParsePosition)
    Object tz3 = fmt.parseObject("++America/Los_Angeles", new ParsePosition(2));
    if (tz3 == null) {
        errln("Parse failure using parseObject(String, ParsePosition) - expected: "
                + expectedTZ.getID());
    } else if (!expectedTZ.equals(tz3)) {
        errln("Parsed TimeZone: '" + ((TimeZone)tz3).getID()
                + "' using parseObject(String, ParsePosition) - expected: "
                + expectedTZ.getID());
    }
}
 
Example 2
Source File: CalendarRegressionTest.java    From j2objc with Apache License 2.0 4 votes vote down vote up
/**
 * Calendar and GregorianCalendar hashCode() methods need improvement.
 * Calendar needs a good implementation that subclasses can override, and
 * GregorianCalendar should use that implementation.
 */
@Test
public void Test4136399() {
    /*
     * Note: This test is actually more strict than it has to be.
     * Technically, there is no requirement that unequal objects have
     * unequal hashes. We only require equal objects to have equal hashes.
     * It is desirable for unequal objects to have distributed hashes, but
     * there is no hard requirement here.
     * 
     * In this test we make assumptions about certain attributes of calendar
     * objects getting represented in the hash, which need not always be the
     * case (although it does work currently with the given test).
     */
    Calendar a = Calendar.getInstance();
    Calendar b = (Calendar)a.clone();
    if (a.hashCode() != b.hashCode()) {
        errln("Calendar hash code unequal for cloned objects");
    }
    TimeZone atz1 = a.getTimeZone();
    TimeZone atz2 = (TimeZone)atz1.clone();
    if(!atz1.equals(atz2)){
        errln("The clone timezones are not equal");
    }
    if(atz1.hashCode()!=atz2.hashCode()){
        errln("TimeZone hash code unequal for cloned objects");
    }
    b.setMinimalDaysInFirstWeek(7 - a.getMinimalDaysInFirstWeek());
    if (a.hashCode() == b.hashCode()) {
        errln("Calendar hash code ignores minimal days in first week");
    }
    b.setMinimalDaysInFirstWeek(a.getMinimalDaysInFirstWeek());

    b.setFirstDayOfWeek((a.getFirstDayOfWeek() % 7) + 1); // Next day
    if (a.hashCode() == b.hashCode()) {
        errln("Calendar hash code ignores first day of week");
    }
    b.setFirstDayOfWeek(a.getFirstDayOfWeek());

    b.setLenient(!a.isLenient());
    if (a.hashCode() == b.hashCode()) {
        errln("Calendar hash code ignores lenient setting");
    }
    b.setLenient(a.isLenient());
    
    // Assume getTimeZone() returns a reference, not a clone
    // of a reference -- this is true as of this writing
    TimeZone atz = a.getTimeZone();
    TimeZone btz = b.getTimeZone();

    btz.setRawOffset(atz.getRawOffset() + 60*60*1000);
    if(atz.hashCode()== btz.hashCode()){
        errln(atz.hashCode()+"=="+btz.hashCode());
    }
    if (a.getTimeZone()!= b.getTimeZone() && a.hashCode() == b.hashCode()) {
        errln("Calendar hash code ignores zone");
    }
    b.getTimeZone().setRawOffset(a.getTimeZone().getRawOffset());

    GregorianCalendar c = new GregorianCalendar();
    GregorianCalendar d = (GregorianCalendar)c.clone();
    if (c.hashCode() != d.hashCode()) {
        errln("GregorianCalendar hash code unequal for clones objects");
    }
    Date cutover = c.getGregorianChange();
    d.setGregorianChange(new Date(cutover.getTime() + 24*60*60*1000));
    if (c.hashCode() == d.hashCode()) {
        errln("GregorianCalendar hash code ignores cutover");
    }        
}