Reputation: 9406
I seem to misunderstand java.util.Date
's conversion from and to java.time.Instance
. I have some legacy code I need to ineract with, and this code uses java.util.Date
in its API.
I am slightly confused when offsets are added in the Date
API. It was my understand that Date
is a UTC time ("the Date class is intended to reflect coordinated universal time (UTC)"), but when I create a Date
from an Instant
an offset is added:
public class TestDateInstant {
@Test
public void instantdate() {
Instant i = Instant.now();
System.out.println(i);
Date d = Date.from(i);
System.out.println(d);
assertThat(i, equalTo(d.toInstant()));
}
}
The assertion holds, but the output on the console is:
2017-09-26T08:24:40.858Z
Tue Sep 26 10:24:40 CEST 2017
I am wondering why Date.from
uses an offset in this case.
Upvotes: 13
Views: 14055
Reputation: 14471
Date
or Instant
both are NOT specific to a timezone.
The difference is when you print them.
Instant.toString()
prints in ISO-8601 representation in UTC timezone.
Date.toString()
prints it in your current timezone.
That's why you see the difference.
Upvotes: 18