0

I've encountered a peculiar issue while using OffsetDateTime.now() in my Java code. On my machine, repeated calls to OffsetDateTime.now() return different values each time, which is what I expect. However, on my colleague's Windows machine, multiple consecutive calls to OffsetDateTime.now() are returning the same value, as if there's a temporary freeze in the values.

Here's a snippet of the test code I'm using:

@Test
void should_() {
    // Given
    for (int i = 0; i < 20; i++) {
        System.out.println("OffsetDateTime.now() = " + OffsetDateTime.now());
    }
    // When

    // Then
}

And here's a sample of the result we're observing on my colleague's machine:

OffsetDateTime.now() = 2023-08-16T12:28:42.125425800+02:00
OffsetDateTime.now() = 2023-08-16T12:28:42.127644700+02:00
OffsetDateTime.now() = 2023-08-16T12:28:42.127644700+02:00
OffsetDateTime.now() = 2023-08-16T12:28:42.127644700+02:00
OffsetDateTime.now() = 2023-08-16T12:28:42.127644700+02:00
OffsetDateTime.now() = 2023-08-16T12:28:42.127644700+02:00
OffsetDateTime.now() = 2023-08-16T12:28:42.127644700+02:00
OffsetDateTime.now() = 2023-08-16T12:28:42.127644700+02:00
OffsetDateTime.now() = 2023-08-16T12:28:42.127644700+02:00
OffsetDateTime.now() = 2023-08-16T12:28:42.127644700+02:00
OffsetDateTime.now() = 2023-08-16T12:28:42.127644700+02:00
OffsetDateTime.now() = 2023-08-16T12:28:42.127644700+02:00
OffsetDateTime.now() = 2023-08-16T12:28:42.128145600+02:00
OffsetDateTime.now() = 2023-08-16T12:28:42.128145600+02:00
OffsetDateTime.now() = 2023-08-16T12:28:42.128145600+02:00
OffsetDateTime.now() = 2023-08-16T12:28:42.128145600+02:00
OffsetDateTime.now() = 2023-08-16T12:28:42.128145600+02:00
OffsetDateTime.now() = 2023-08-16T12:28:42.128145600+02:00
OffsetDateTime.now() = 2023-08-16T12:28:42.128145600+02:00
OffsetDateTime.now() = 2023-08-16T12:28:42.128145600+02:00

Why might this be happening and how could I effectively handle this issue?

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
Reda Red
  • 3
  • 3
  • 2
    The best clock available on your colleague's machine is just not as good as yours. It is not required that `now` should give you a different value every time you call it. Just don't expect this behaviour and you're fine. – Sweeper Aug 16 '23 at 13:04
  • It's not even required that OffsetDateTime.now() give you an ascending value over time. (And not likely that it does 100% of the time.) – Louis Wasserman Aug 16 '23 at 16:07

0 Answers0