JDK-8367901 : Calendar.roll(hour, 24) returns wrong result
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.util:i18n
  • Affected Version: 11,25
  • Priority: P4
  • Status: Resolved
  • Resolution: Fixed
  • OS: generic
  • CPU: generic
  • Submitted: 2025-09-17
  • Updated: 2025-09-22
  • Resolved: 2025-09-22
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 26
26 masterFixed
Related Reports
Causes :  
Description
A DESCRIPTION OF THE PROBLEM :
Mon Sep 12 15:30:45 GMT 2005 roll(HOUR,24) get Mon Sep 12 16:30:45 GMT 2005.
I think the correct result should be Mon Sep 12 15:30:45 GMT 2005.


EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
Mon Sep 12 15:30:45 GMT 2005
Mon Sep 12 14:30:45 GMT 2005
Mon Sep 12 15:30:45 GMT 2005
Mon Sep 12 15:30:45 GMT 2005
ACTUAL -
Mon Sep 12 15:30:45 GMT 2005
Mon Sep 12 14:30:45 GMT 2005
Mon Sep 12 15:30:45 GMT 2005
Mon Sep 12 16:30:45 GMT 2005

---------- BEGIN SOURCE ----------


import java.util.GregorianCalendar;
import static java.util.Calendar.HOUR;
import java.util.Locale;
import java.util.TimeZone;

public class DateRollTest{
    public static void main(String[] args) {
        TimeZone.setDefault(TimeZone.getTimeZone("GMT"));
        GregorianCalendar cal = new GregorianCalendar(TimeZone.getTimeZone("GMT"), Locale.US);
        cal.clear();
        cal.set(2005, 8, 12, 15, 30, 45);
        System.out.println(cal.getTime());
        cal.roll(HOUR, 23);
        System.out.println(cal.getTime());


        cal.clear();
        cal.set(2005, 8, 12, 15, 30, 45);
        System.out.println(cal.getTime());
        cal.roll(HOUR, 24);
        System.out.println(cal.getTime());
    }

}
---------- END SOURCE ----------


Comments
Changeset: ced3f13f Branch: master Author: Justin Lu <jlu@openjdk.org> Date: 2025-09-22 16:58:00 +0000 URL: https://git.openjdk.org/jdk/commit/ced3f13f4e036513444d1fea3958be11741c2b8e
22-09-2025

Calendar rolling is incorrect when the rolled amount would set the hour to the hour before rolling occurred. For example, 24 hours, 48 hours, etc. This will be addressed, but users are always suggested to migrate to the java.time alternatives.
18-09-2025

A pull request was submitted for review. Branch: master URL: https://git.openjdk.org/jdk/pull/27355 Date: 2025-09-17 22:37:52 +0000
17-09-2025

It appears that rolling by 24 and 25 hours produce the same results. JDK 20.0.2 / JDK 26 Dev cal.set(2005, 8, 12, 15, 30, 45); cal.roll(HOUR, 24); System.out.println(cal.getTime()); cal.clear(); cal.set(2005, 8, 12, 15, 30, 45); cal.roll(HOUR, 25); System.out.println(cal.getTime()); > Mon Sep 12 16:30:45 GMT 2005 > Mon Sep 12 16:30:45 GMT 2005
17-09-2025

Impact -> M (Somewhere in-between the extremes) Likelihood -> M (while using Calendar.roll(HOUR, 24)) Workaround -> L (avoid problematic use) Priority -> P4
17-09-2025

Observation on Windows 11 ------------------------------------------ JDK 8u471 : Passed (Calendar.roll(HOUR, 24) returns 'Mon Sep 12 15:30:45 GMT 2005') JDK 11.0.29 : Failed (Calendar.roll(HOUR, 24) returns 'Mon Sep 12 16:30:45 GMT 2005') JDK 25.0.1ea: Failed (Calendar.roll(HOUR, 24) returns 'Mon Sep 12 17:30:45 GMT 2005')
17-09-2025