Display short timezone name using DateTimeFormatter

I have the following DateTimeFormatter .

 DateTimeFormatter DATE_TIME_FORMATTER = DateTimeFormatter.ofPattern("MM/dd/yyyy 'at' hh:mm:ss a zzzz"); 

I use it to format ZonedDateTime as follows:

 ZonedDateTime disableTime = Instant.now() .plus(Duration.ofDays(21)) .atZone(ZoneId.ofOffset("UTC", ZoneOffset.ofHours(-5))); System.out.println(DATE_TIME_FORMATTER.format(disableTime)); 

I would like this to create a formatted date string, for example the following:

09/16/2015 at 01:15:45 PM EDT

But what happens:

09/16/2015 at 01:15:45 PM UTC-05: 00

Regardless of whether I use z , zz , zzz or zzzz in the template, it is always displayed in the above format.

Is there any other way to create a ZonedDateTime that will give me the desired result, or am I doing something wrong in the template?

According to the documentation, DateTimeFormatter O should be used to display a localized zone offset, such as UTC-05:00 , and z should be used to display a time zone name, such as Eastern Daylight Time or EDT .

+8
java java-8 java-time
source share
1 answer

This is because you are using an anonymous UTC offset. Try instead with the name ZoneId

 ZonedDateTime disableTime = Instant.now() .plus(Duration.ofDays(21)) .atZone(ZoneId.of("Africa/Nairobi")); 

prints ("Ora dell'Africa orientale" are Italian localized names)

 09/16/2015 at 09:34:44 PM Ora dell'Africa orientale 

You can get a list of available names that should be used to store and get preferences from the user with

 Set<String> ids = ZoneId.getAvailableZoneIds(); 
+6
source share

All Articles