I have an application where I use milliseconds since Epoch to convert to LocalDateTime and back, the application was working well on Java 8 but I tried to update to Java 10 and found the following issue
This is the output in Java 8
✘ magg@MacBook-Pro-de-Miguel ~/Desktop/CODE java TimeTest
2018-09-06T20:13:30.253
1536290010253
2018-09-06T20:13:30.253
✘ magg@MacBook-Pro-de-Miguel ~/Desktop/CODE java -version
java version "1.8.0_181"
Java(TM) SE Runtime Environment (build 1.8.0_181-b13)
Java HotSpot(TM) 64-Bit Server VM (build 25.181-b13, mixed mode)
This is the output in Java 10, if you see the format is 6 digits long at the end not three like in Java 8 and this breaks the conversion from milliseconds since Epoch to LocalDateTime
✘ magg@MacBook-Pro-de-Miguel ~/Desktop/CODE java TimeTest
2018-09-06T20:13:18.568414
1536289998568
2018-09-06T20:13:18.568
✘ magg@MacBook-Pro-de-Miguel ~/Desktop/CODE java -version
java version "10.0.2" 2018-07-17
Java(TM) SE Runtime Environment 18.3 (build 10.0.2+13)
Java HotSpot(TM) 64-Bit Server VM 18.3 (build 10.0.2+13, mixed mode)
Here is the source code
public class TimeTest{
public static void main(String[] args)
{
TimeZone.setDefault(TimeZone.getTimeZone("America/Los_Angeles"));
LocalDateTime issueTime = LocalDateTime.now();
System.out.println(issueTime.toString());
ZonedDateTime zdt = issueTime.atZone(TimeZone.getDefault().toZoneId());
long timeInMillis = zdt.toInstant().toEpochMilli();
System.out.println(timeInMillis );
LocalDateTime date = LocalDateTime.ofInstant(Instant.ofEpochMilli(timeInMillis), TimeZone.getDefault().toZoneId());
System.out.println(date.toString());
}
}
How can I fix this in Java 10?