The LocalDateTime API gives the possibility to add the TimeZone Name by using the key \"z\" in the formatter. I get an exception adding this key and don\'t understand why. I
The LocalDateTime
class does not support time zones; you can use ZonedDateTime instead.
ZonedDateTime now = ZonedDateTime.now();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("hh:mm:ss a z");
System.out.println(now.format(formatter));
This no longer throws an exception and prints 06:08:20 PM EDT
for me, but the timezone will differ depending on your location.