Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 19 additions & 8 deletions src/java.base/share/classes/java/util/SimpleTimeZone.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 1996, 2024, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1996, 2025, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -112,15 +112,15 @@
* <pre><code>
* // Base GMT offset: -8:00
* // DST starts: at 2:00am in standard time
* // on the first Sunday in April
* // on the second Sunday in March
* // DST ends: at 2:00am in daylight time
* // on the last Sunday in October
* // on the first Sunday in November
* // Save: 1 hour
* SimpleTimeZone(-28800000,
* "America/Los_Angeles",
* Calendar.APRIL, 1, -Calendar.SUNDAY,
* Calendar.MARCH, 8, -Calendar.SUNDAY,
* 7200000,
* Calendar.OCTOBER, -1, Calendar.SUNDAY,
* Calendar.NOVEMBER, 1, -Calendar.SUNDAY,
* 7200000,
* 3600000)
*
Expand Down Expand Up @@ -863,13 +863,24 @@ public Object clone()
}

/**
* Generates the hash code for the SimpleDateFormat object.
* Generates the hash code for the SimpleTimeZone object.
* @return the hash code for this object
*/
public int hashCode()
{
return startMonth ^ startDay ^ startDayOfWeek ^ startTime ^
endMonth ^ endDay ^ endDayOfWeek ^ endTime ^ rawOffset;
int hash = 31 * getID().hashCode() + rawOffset;
hash = 31 * hash + Boolean.hashCode(useDaylight);
if (useDaylight) {
hash = 31 * hash + startMonth;
hash = 31 * hash + startDay;
hash = 31 * hash + startDayOfWeek;
hash = 31 * hash + startTime;
hash = 31 * hash + endMonth;
hash = 31 * hash + endDay;
hash = 31 * hash + endDayOfWeek;
hash = 31 * hash + endTime;
}
return hash;
}

/**
Expand Down
78 changes: 78 additions & 0 deletions test/jdk/java/util/TimeZone/SimpleTimeZoneHashCodeTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*
* Copyright (c) 2025, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/

/*
* @test
* @bug 8369184
* @summary Checks if equals()/hashCode() of SimpleTimeZone works correctly
* @run junit SimpleTimeZoneHashCodeTest
*/

import java.util.SimpleTimeZone;
import static java.util.Calendar.MARCH;
import static java.util.Calendar.NOVEMBER;
import static java.util.Calendar.SUNDAY;

import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

public class SimpleTimeZoneHashCodeTest {
private static final SimpleTimeZone STZ_WITH_DST =
new SimpleTimeZone(-288_000_000, "America/Los_Angeles",
MARCH, 8, -SUNDAY, 7_200_000,
NOVEMBER, 1, -SUNDAY, 7_200_000);
private static final SimpleTimeZone STZ_WITHOUT_DST =
new SimpleTimeZone(0, "foo");

@Test
void withDSTTest() {
var stz = (SimpleTimeZone)STZ_WITH_DST.clone();
assertEquals(STZ_WITH_DST, stz);
assertEquals(STZ_WITH_DST.hashCode(), stz.hashCode());

stz.setEndRule(NOVEMBER, 8, -SUNDAY, 7_200_000);
assertNotEquals(STZ_WITH_DST, stz);
// from the contract point, hash codes may be the same
assertNotEquals(STZ_WITH_DST.hashCode(), stz.hashCode());
}

@Test
void withOutDSTTest() {
var stz = (SimpleTimeZone)STZ_WITHOUT_DST.clone();

// Only setting start rule. Still considered non-DST zone
stz.setStartRule(MARCH, 8, -SUNDAY, 7_200_000);
assertTrue(!stz.useDaylightTime());
assertEquals(STZ_WITHOUT_DST, stz);
assertEquals(STZ_WITHOUT_DST.hashCode(), stz.hashCode());

// Setting end rule as well. Now it is considered DST zone
stz.setEndRule(NOVEMBER, 8, -SUNDAY, 7_200_000);
assertTrue(stz.useDaylightTime());
assertNotEquals(STZ_WITHOUT_DST, stz);
assertNotEquals(STZ_WITHOUT_DST.hashCode(), stz.hashCode());
}
}