简体   繁体   中英

Java/Joda: Get TimeZone from long name (Display Name)

In receiving iCal events from Outlook, it is specifying the timezone in the long format:

DTSTART;TZID=Eastern Standard Time:20120521T180000

That is, what you'd get from TimeZone.getDisplayName().

How can I go from that ( Eastern Standard Time ) to a TimeZone object?

Well, it ain't pretty, but here's what I ended up with (basically, iterate over available TZ's, searching for a getDisplayName() match):

// NOTE: vTZID is the timezone ID as presented by the iCalendar format // For example 'Eastern Standard Time'. I only do this if the TZ cannot be found by the normal method // (that is, TimeZone.getTimeZone())

boolean foundTZ=false;
for (String availId : TimeZone.getAvailableIDs()){
  if (vTZID.trim().equalsIgnoreCase(TimeZone.getTimeZone(availId).getDisplayName())){
    vTimezone = TimeZone.getTimeZone(availId);
    foundTZ = true;
    break;
  }
}

have you looked at olson time zone ? also available in joda: http://joda-time.sourceforge.net/timezones.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM