Before Java-8 I got accustomed to always keep anything date/time related as milliseconds since Epoch and only ever deal with human readable dates/times on the way out, i.e. in a UI or a log file, or when parsing user generated input.
I think this is still safe with Java-8, and now I am looking for the most concise way to get a formatted date out of a milliseconds time stamp. I tried
df = Dateformatter.ofPattern("...pattern...");
Unsupported field: YearOfEra
The error you have when formatting an
Instant using a formatter built with a year or other fields is expected; an
Instant does not know which year or month or day it is, it only knows how much milliseconds have elapsed since the Epoch. For the same instant, it could be 2 different days on 2 different places of the Earth.
So you need to add a time zone information if you want to print the day. With an
Instant, you can call
atZone(zone) to combine it with a
ZoneId in order to form a
ZonedDateTime. This is very much like an instant, only that it has a time zone information. If you want to use the system time zone (the one of the running VM), you can get it with
To print it, you can use the two built-in formatter
ISO_ZONED_DATE_TIME. The difference between the two is that the zoned date time formatter will add the zone id to the output.
Instant instant = Instant.now(); DateTimeFormatter formatter = DateTimeFormatter.ISO_OFFSET_DATE_TIME; System.out.println(formatter.format(instant.atZone(ZoneId.systemDefault()))); System.out.println(formatter.format(instant.atZone(ZoneId.of("America/Los_Angeles"))));
when run on my machine, which has a system time zone of
"Europe/Paris", you'll get: