Skip to content

Commit 1b32d8d

Browse files
zhaomengsbrannen
authored andcommitted
Include zone ID in CronTrigger's equals() and hashCode() implementations
CronTrigger carries an optional ZoneId since 5.3 that affects nextExecution; however, prior to this commit, equals() and hashCode() only considered the cron expression. This commit ensures that CronTrigger instances with the same cron expression but different time zones are no longer considered equal. Closes gh-36871 Signed-off-by: zhaomeng <zhaomeng1.vendor@sensetime.com>
1 parent b4a3781 commit 1b32d8d

2 files changed

Lines changed: 15 additions & 2 deletions

File tree

spring-context/src/main/java/org/springframework/scheduling/support/CronTrigger.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import java.time.Instant;
2020
import java.time.ZoneId;
2121
import java.time.ZonedDateTime;
22+
import java.util.Objects;
2223
import java.util.TimeZone;
2324

2425
import org.jspecify.annotations.Nullable;
@@ -145,12 +146,13 @@ Instant determineInitialTimestamp(TriggerContext triggerContext) {
145146
@Override
146147
public boolean equals(@Nullable Object other) {
147148
return (this == other || (other instanceof CronTrigger that &&
148-
this.expression.equals(that.expression)));
149+
this.expression.equals(that.expression) &&
150+
Objects.equals(this.zoneId, that.zoneId)));
149151
}
150152

151153
@Override
152154
public int hashCode() {
153-
return this.expression.hashCode();
155+
return Objects.hash(this.expression, this.zoneId);
154156
}
155157

156158
@Override

spring-context/src/test/java/org/springframework/scheduling/support/CronTriggerTests.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package org.springframework.scheduling.support;
1818

19+
import java.time.ZoneId;
1920
import java.util.Calendar;
2021
import java.util.Date;
2122
import java.util.GregorianCalendar;
@@ -746,6 +747,16 @@ void daylightSavingMissingHour() {
746747
assertThat(nextExecutionTime).isEqualTo(this.calendar.getTime());
747748
}
748749

750+
@Test
751+
void equalsAndHashCodeConsiderZoneId() {
752+
String cron = "0 0 9 * * *";
753+
CronTrigger amsterdam = new CronTrigger(cron, ZoneId.of("Europe/Amsterdam"));
754+
CronTrigger newYork = new CronTrigger(cron, ZoneId.of("America/New_York"));
755+
756+
assertThat(amsterdam).isNotEqualTo(newYork);
757+
assertThat(amsterdam).doesNotHaveSameHashCodeAs(newYork);
758+
}
759+
749760

750761
private static void roundup(Calendar calendar) {
751762
calendar.add(Calendar.SECOND, 1);

0 commit comments

Comments
 (0)