I suggest to cast to a LocalDateTime
and then use DateTimeFormatter
like
LocalDateTime ldt = LocalDateTime.ofInstant(instant, ZoneOffset.UTC);
String result = ldt.format(DateTimeFormatter.ISO_LOCAL_DATE);
You can find formatter list here, or use your own pattern
Other ways
As @Holger said in the comments, DateTimeFormatter.ISO_LOCAL_DATE.withZone(ZoneOffset.UTC)
produces a formatter that can directly format the Instant
and can be shared and reused to format a lot of instants.
static final DateTimeFormatter INSTANT_FORMATTER
= DateTimeFormatter.ISO_LOCAL_DATE.withZone(ZoneOffset.UTC);
Use like
String result = INSTANT_FORMATTER .format(instant);
@Ole V.V. mentioned that converting to LocalDateTime
as in my first snippet throws away the information about which point in time we had. We can keep that information with us when instead we convert to ZonedDateTime
or OffsetDateTime
. If you want to interpret the date in UTC (as in the previous examples), for example:
String result = instant.atOffset(ZoneOffset.UTC).format(DateTimeFormatter.ISO_LOCAL_DATE);
This makes it more explicit that the operation is dependent on the choice of UTC for interpretation.