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>
This commit is contained in:
zhaomeng
2026-06-04 10:16:06 +08:00
committed by Sam Brannen
parent b4a378186f
commit 1b32d8d41d
2 changed files with 15 additions and 2 deletions
@@ -19,6 +19,7 @@ package org.springframework.scheduling.support;
import java.time.Instant;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.Objects;
import java.util.TimeZone;
import org.jspecify.annotations.Nullable;
@@ -145,12 +146,13 @@ public class CronTrigger implements Trigger {
@Override
public boolean equals(@Nullable Object other) {
return (this == other || (other instanceof CronTrigger that &&
this.expression.equals(that.expression)));
this.expression.equals(that.expression) &&
Objects.equals(this.zoneId, that.zoneId)));
}
@Override
public int hashCode() {
return this.expression.hashCode();
return Objects.hash(this.expression, this.zoneId);
}
@Override
@@ -16,6 +16,7 @@
package org.springframework.scheduling.support;
import java.time.ZoneId;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
@@ -746,6 +747,16 @@ class CronTriggerTests {
assertThat(nextExecutionTime).isEqualTo(this.calendar.getTime());
}
@Test
void equalsAndHashCodeConsiderZoneId() {
String cron = "0 0 9 * * *";
CronTrigger amsterdam = new CronTrigger(cron, ZoneId.of("Europe/Amsterdam"));
CronTrigger newYork = new CronTrigger(cron, ZoneId.of("America/New_York"));
assertThat(amsterdam).isNotEqualTo(newYork);
assertThat(amsterdam).doesNotHaveSameHashCodeAs(newYork);
}
private static void roundup(Calendar calendar) {
calendar.add(Calendar.SECOND, 1);