1

I have ZonedDateTime instance, trying to get zone as string (e.g. EST/EDT) like this:

merchantLocalReceiptDateTime.getZone().getDisplayName(TextStyle.SHORT, Locale.getDefault())

For my setup, it returns me EST while in fact I was expecting EDT. Pls advise how to get zone string that correctly reflects daylight saving.

Kanagavelu Sugumar
  • 18,766
  • 20
  • 94
  • 101
ror
  • 3,295
  • 1
  • 19
  • 27

3 Answers3

0
     static DateTimeFormatter etFormat = DateTimeFormatter.ofPattern("MM/dd/yyyy 'at' hh:mma 'ET'");

      static ZoneId istZoneId = ZoneId.of("Asia/Kolkata");
      static ZoneId etZoneId = ZoneId.of("America/New_York");

 LocalDateTime currentDateTime = LocalDateTime.now();

    ZonedDateTime currentISTime = currentDateTime.atZone(istZoneId);                
    ZonedDateTime currentETime = currentISTime.withZoneSameInstant(etZoneId);       //ET Time

    System.out.println(etFormat.format(currentETime));
Dhara Jani
  • 461
  • 3
  • 10
  • Thanks, I'm using threetenbp so cannot use the above as an answer but even if I would, the above would give me more then one hit I guess? – ror Jul 18 '19 at 13:38
  • May be you got help from this : https://github.com/JakeWharton/ThreeTenABP – Dhara Jani Jul 18 '19 at 13:48
  • Yes I already have ZoneDateTime instance that's created based on instant and ZoneId information. Problem is not about creating ZoneDateTime, but rather reading its zone information - in user friendly way, respecting daylight if applicable. – ror Jul 18 '19 at 13:51
  • @ror check updated answer now ... i hope it will now work for you ! – Dhara Jani Jul 24 '19 at 10:23
  • Appreciate your effort! Ran just now, it gives me: 07/24/2019 at 04:59AM ET however what I'm trying to accomplish is get timezone. For New York's 'now' it should be EDT, however even with your method Im getting EST - that's the problem. I'm puzzled why it's soo difficult in android to work with dates. – ror Jul 24 '19 at 11:30
  • Ok before getting result write this line : TimeZone.setDefault(TimeZone.getTimeZone("EST")); and then print this : TimeZone.getDefault() – Dhara Jani Jul 24 '19 at 11:48
  • This certainly does not help. – ror Jul 24 '19 at 14:07
0

Ok, I do not like this solution but it is the only one I came up with so far, the only one that works:

So we have preinitialized ZonedDateTime merchantLocalReceiptDateTime (remember, this is threetenbp). We can do this then (is it daylight saving for given time?):

boolean isDaylightSaving = merchantLocalReceiptDateTime.getZone()
                .getRules().isDaylightSavings(merchantLocalReceiptDateTime.toInstant());

Then, to get short representation of timezone that respects daylight saving, we can do this (TimeZone is not part of threeten, it is java.util):

TimeZone.getTimeZone(merchantLocalReceiptDateTime.getZone().getId())
                .getDisplayName(isDaylightSaving, TimeZone.SHORT)

For New York (assuming device language is English/United States), the above produces EST in winter and EDT right now. For timezones without particular daylight saving names, it can give, for example, "GMT+2", "GMT+3" etc. If language is different, you will likely get those "GMT+-".

ror
  • 3,295
  • 1
  • 19
  • 27
0
//SHORT: CEST
DateTimeFormatter.ofPattern("zzz").format(zonedDateTime)

//SHORT: CET 
ZoneId.getDisplayName(TextStyle.SHORT,Locale.ENGLISH)    

//LONG: Central European Summer Time
DateTimeFormatter.ofPattern("zzzz").format(zonedDateTime)

//LONG: Central European Time
ZoneId.getDisplayName(TextStyle.LONG,Locale.ENGLISH)

//Use this for converting CET to CEST and vice versa
TimeZone tz = TimeZone.getTimeZone(timeZone.getZone());
tz.getDisplayName(true, TimeZone.SHORT, Locale.ENGLISH));
Kanagavelu Sugumar
  • 18,766
  • 20
  • 94
  • 101