Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8338746

Time returned by ZonedDateTime.now() can go backwards

XMLWordPrintable

    • aarch64
    • os_x

      ADDITIONAL SYSTEM INFORMATION :
      Software:
          System Software Overview:
            System Version: macOS 14.5 (23F79)
            Kernel Version: Darwin 23.5.0
            Boot Volume: Macintosh HD
            Boot Mode: Normal
            Secure Virtual Memory: Enabled
            System Integrity Protection: Enabled
            Time since boot: 7 days, 44 minutes

      Hardware:
          Hardware Overview:
            Model Name: MacBook Pro
            Model Identifier: Mac15,9
            Model Number: Z1CM000BRCZ/A
            Chip: Apple M3 Max
            Total Number of Cores: 16 (12 performance and 4 efficiency)
            Memory: 128 GB
            System Firmware Version: 10151.121.1
            OS Loader Version: 10151.121.1
            Activation Lock Status: Enabled

      openjdk version "21.0.2" 2024-01-16
      OpenJDK Runtime Environment (build 21.0.2+13-58)
      OpenJDK 64-Bit Server VM (build 21.0.2+13-58, mixed mode, sharing)
      VM settings:
          Max. Heap Size (Estimated): 29.50G
          Using VM: OpenJDK 64-Bit Server VM

      Property settings:
          file.encoding = UTF-8
          file.separator = /
          ftp.nonProxyHosts = local|*.local|169.254/16|*.169.254/16
          http.nonProxyHosts = local|*.local|169.254/16|*.169.254/16
          java.class.path = .
              /usr/local/lib/antlr-4.13.1-complete.jar
          java.class.version = 65.0
          java.home = /Users/uuDev/.sdkman/candidates/java/21.0.2-open
          java.io.tmpdir = /var/folders/bm/ycht_2757mxczsk7tn5dzzh00000gp/T/
          java.library.path = /Users/uuDev/Library/Java/Extensions
              /Library/Java/Extensions
              /Network/Library/Java/Extensions
              /System/Library/Java/Extensions
              /usr/lib/java
              .
          java.runtime.name = OpenJDK Runtime Environment
          java.runtime.version = 21.0.2+13-58
          java.specification.name = Java Platform API Specification
          java.specification.vendor = Oracle Corporation
          java.specification.version = 21
          java.vendor = Oracle Corporation
          java.vendor.url = https://java.oracle.com/
          java.vendor.url.bug = https://bugreport.java.com/bugreport/
          java.version = 21.0.2
          java.version.date = 2024-01-16
          java.vm.compressedOopsMode = Non-zero based
          java.vm.info = mixed mode, sharing
          java.vm.name = OpenJDK 64-Bit Server VM
          java.vm.specification.name = Java Virtual Machine Specification
          java.vm.specification.vendor = Oracle Corporation
          java.vm.specification.version = 21
          java.vm.vendor = Oracle Corporation
          java.vm.version = 21.0.2+13-58
          jdk.debug = release
          line.separator = \n
          native.encoding = UTF-8
          os.arch = aarch64
          os.name = Mac OS X
          os.version = 14.5
          path.separator = :
          socksNonProxyHosts = local|*.local|169.254/16|*.169.254/16
          stderr.encoding = UTF-8
          stdout.encoding = UTF-8
          sun.arch.data.model = 64
          sun.boot.library.path = /Users/uuDev/.sdkman/candidates/java/21.0.2-open/lib
          sun.cpu.endian = little
          sun.io.unicode.encoding = UnicodeBig
          sun.java.launcher = SUN_STANDARD
          sun.jnu.encoding = UTF-8
          sun.management.compiler = HotSpot 64-Bit Tiered Compilers
          user.country = US
          user.language = en

      Locale settings:
          default locale = English (United States)
          default display locale = English (United States)
          default format locale = English (United States)
          tzdata version = 2023c
       

      A DESCRIPTION OF THE PROBLEM :
      If I invoke ZonedDateTime.now() twice sequentially, the second invocation sometimes returns time before the first one. See test case below.

      I've observed this behavior only on Apple hardware with Apple M CPU. It does not happen on other operating systems (Windows, Linux) with Intel x64 CPUs.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Run ZonedDateTime.now() twice in a sequence and compare the returned values. See test case below for more details.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      Second invocation of ZonedDateTime.now() returns value, that is the same as the first one or after the first one.
      ACTUAL -
      In some cases, second invocation returns value before the first one.

      You need to repeat the sequence multiple times before it happens. On my machine, it usually happens within a couple of milliseconds.

      ---------- BEGIN SOURCE ----------
      package bug.java.now;

      import java.time.ZonedDateTime;
      import org.junit.jupiter.api.Test;

      class NowTest {

        @Test
        void test() {
          while (true) {
            var t1 = ZonedDateTime.now();
            var t2 = ZonedDateTime.now();
            if (t2.isBefore(t1)) {
              throw new RuntimeException("Is before " + t2 + " " + t1);
            }
          }
        }
      }
      ---------- END SOURCE ----------

      FREQUENCY : always

            dholmes David Holmes
            pnarayanaswa Praveen Narayanaswamy
            Votes:
            0 Vote for this issue
            Watchers:
            5 Start watching this issue

              Created:
              Updated:
              Resolved: