Reputation: 12245
I execute the following code and I got a different precision between Windows and Unix (macOs).
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
public class TimeTest {
public static void main(String[] args) {
System.out.println(DateTimeFormatter.ISO_OFFSET_DATE_TIME.format(ZonedDateTime.now()));
}
}
Output on MacOS (Darwin Nicolass-MacBook-Pro.local 17.3.0 Darwin Kernel Version 17.3.0: Thu Nov 9 18:09:22 PST 2017; root:xnu-4570.31.3~1/RELEASE_X86_64 x86_64
) is
> java -version
java version "1.8.0_162"
Java(TM) SE Runtime Environment (build 1.8.0_162-b12)
Java HotSpot(TM) 64-Bit Server VM (build 25.162-b12, mixed mode)
> java TimeTest
2018-01-31T11:05:49.59+01:00
while on Windows
>java -version
java version "1.8.0_161"
Java(TM) SE Runtime Environment (build 1.8.0_161-b12)
Java HotSpot(TM) 64-Bit Server VM (build 25.161-b12, mixed mode)
>java TimeTest
2018-01-31T11:02:22.452+01:00
Is there an expected output or a bug?
Upvotes: 1
Views: 582
Reputation: 1502176
I suspect you just happened to run at an instant where the time was on a 10ms boundary on the Mac.
Here's some sample code to try:
import java.time.*;
import java.time.format.*;
class Test {
public static void main(String[] args) throws Exception {
DateTimeFormatter formatter = DateTimeFormatter.ISO_OFFSET_DATE_TIME;
String last = "";
while (true) {
String next = formatter.format(ZonedDateTime.now());
if (!last.equals(next)) {
last = next;
System.out.println(next);
}
}
}
}
The output on my Windows box shows a mixture of output lengths. For example:
2018-01-31T10:17:34.589Z
2018-01-31T10:17:34.59Z
2018-01-31T10:17:34.591Z
2018-01-31T10:17:34.592Z
2018-01-31T10:17:34.593Z
2018-01-31T10:17:34.594Z
2018-01-31T10:17:34.595Z
2018-01-31T10:17:34.596Z
2018-01-31T10:17:34.597Z
2018-01-31T10:17:34.598Z
2018-01-31T10:17:34.599Z
2018-01-31T10:17:34.6Z
2018-01-31T10:17:34.601Z
I suspect if you run that code in your various environments, you'll see the same result.
If you need the same length for all output, I think you'll need to specify a custom pattern to DateTimeFormatter.ofPattern(String, Locale)
.
Upvotes: 3