The java.util.Date toString() method displays the date in the local time zone.
There are several common scenarios where we want the data to be printed in
Just use Instant of java.time.
System.out.println(Instant.now());
This just printed:
2018-01-27T09:35:23.179612Z
Instant.toString always gives UTC time.
The output is usually sortable, but there are unfortunate exceptions. toString gives you enough groups of three decimals to render the precision it holds. On the Java 9 on my Mac the precision of Instant.now() seems to be microseconds, but we should expect that in approximately one case out of a thousand it will hit a whole number of milliseconds and print only three decimals. Strings with unequal numbers of decimals will be sorted in the wrong order (unless you write a custom comparator to take this into account).
Instant is one of the classes in java.time, the modern Java date and time API, which I warmly recommend that you use instead of the outdated Date class. java.time is built into Java 8 and later and has also been backported to Java 6 and 7.