java 8 instant with time 00:00:00 gives time 02:00:00 after conversion to util,Date

1.1k Views Asked by At

I have a problem in converting java.time.Instant to java.util.Date:

System.out.println(item.getStart()); // getStart returns instant
System.out.println(Date.from(item.getStart()));

gives output:

2017-08-29T00:00:00Z
Tue Aug 29 02:00:00 CEST 2017

Why in date time is 02:00:00? And how to do convertion with time 00:00:00

1

There are 1 best solutions below

1
On

In 2017-08-29T00:00:00Z the Z means Zulu, which means UTC+0. So it describtes exactly the same moment in time as Tue Aug 29 02:00:00 CEST 2017 which is UTC+2

It gets printed as CEST because the default implementation of the toString() method is using the default time zone.

You could try using Java8 LocalDate instead. It hasn’t got any time-of-day information.

How to do the conversion you can see here: https://stackoverflow.com/a/35187046/1199731