Timezone display value with GMT offset from a give

2019-07-14 18:21发布

I would like to construct a timezone list to show it to users to select. The display name has to be like:

( GMT 5:30 ) India Standard Time(Asia/Calcutta)

I am taking all timezones with TimeZone.getAvailableIDs() and constructing the list. The code I wrote is:

String[] timeZones = TimeZone.getAvailableIDs();
List<String> tzList = new ArrayList<String>();
for (String timeZone : timeZones)
{
  TimeZone tz = TimeZone.getTimeZone(timeZone);
  StringBuilder timeZoneStr = new StringBuilder();
  timeZoneStr.append("( GMT ").append(tz.getRawOffset() / (60 * 60 * 1000)).append(" ) ").append(tz.getDisplayName()).append("(").append(timeZone).append(")");
  tzList.add(timeZoneStr.toString());
  System.out.println(timeZoneStr.toString());
}

A snippet of the output would be like:

( GMT 5 ) Maldives Time(Indian/Maldives)
( GMT 5 ) Pakistan Time(PLT)
( GMT 5 ) India Standard Time(Asia/Calcutta)
( GMT 5 ) India Standard Time(Asia/Kolkata)
( GMT 5 ) India Standard Time(IST)

But the output I need to get is:

( GMT 5:0 ) Maldives Time(Indian/Maldives)
( GMT 5:0 ) Pakistan Time(PLT)
( GMT 5:30 ) India Standard Time(Asia/Calcutta)
( GMT 5:30 ) India Standard Time(Asia/Kolkata)
( GMT 5:30 ) India Standard Time(IST)

What should I do to get 5:30?

标签: java timezone
5条回答
何必那么认真
2楼-- · 2019-07-14 19:03

Another solution with the proper formatting :

DateFormat fmt = new SimpleDateFormat("HH:mm");
fmt.setTimeZone(TimeZone.getTimeZone("UTC"));

List<String> tzList = new ArrayList<String>();
for (String timeZone : TimeZone.getAvailableIDs()) {   
    TimeZone tz = TimeZone.getTimeZone(timeZone);

    StringBuilder timeZoneStr = new StringBuilder();
    timeZoneStr.append("( GMT ");
    if (tz.getRawOffset() < 0) {
        timeZoneStr.append("-");
    }
    timeZoneStr.append(fmt.format(new Date(Math.abs(tz.getRawOffset()))));
    timeZoneStr.append(" ) ").append(tz.getDisplayName()).append(timeZone).append(")");

    tzList.add(timeZoneStr.toString());
    System.out.println(timeZoneStr.toString());
}
查看更多
看我几分像从前
3楼-- · 2019-07-14 19:12

Just use a SimpleDateFormat with the XXX timezone pattern (produces an ISO 8601 timezone)

    DateFormat gmt = new SimpleDateFormat("'GMT' XXX");
    for (TimeZone tz : timeZones) {
        gmt.setTimeZone(tz);
        String gmtTimezone = gmt.format(new Date());
        System.err.println(tz.getID() + " " + gmtTimezone);
    }

Source: http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html#iso8601timezone

查看更多
forever°为你锁心
4楼-- · 2019-07-14 19:15

You should switch the timezone forming line to:

timeZoneStr.append("( GMT ").append(tz.getRawOffset() / (60 * 60 * 1000)).append(":").append((tz.getRawOffset() / (60 * 1000))%60).append(" ) ").append(tz.getDisplayName()).append("(").append(timeZone).append(")");

I think this is the answer you're looking for. However, you may still format the minutes, as they only show one digit when the time offset is, for example 12 hours. It's shown as 12:0.

查看更多
三岁会撩人
5楼-- · 2019-07-14 19:21

A more readable answer is the following:

for (String timeZone : timeZones) {
  TimeZone tz = TimeZone.getTimeZone(timeZone);

  long hours = TimeUnit.MILLISECONDS.toHours(tz.getRawOffset());
  long minutes = TimeUnit.MILLISECONDS.toMinutes(tz.getRawOffset())
      - TimeUnit.HOURS.toMinutes(hours);

  String timeZoneString = String.format("( GMT %d:%02d ) %s(%s)", hours,
      minutes, tz.getDisplayName(), timeZone);
  tzList.add(timeZoneString);
  System.out.println(timeZoneString);
}

This also correctly displays e.g. 5:00 and 5:30. The use of String.format() makes the final string easier to determine when reading the code. The use of the TimeUnit class simplifies the maths.

查看更多
相关推荐>>
6楼-- · 2019-07-14 19:28

Offset timezone using below method :

private static String displayTimeZone(TimeZone tz) {

    long hours = TimeUnit.MILLISECONDS.toHours(tz.getRawOffset());
    long minutes = TimeUnit.MILLISECONDS.toMinutes(tz.getRawOffset()) 
                              - TimeUnit.HOURS.toMinutes(hours);
    // avoid -4:-30 issue
    minutes = Math.abs(minutes);

    String result = "";
    if (hours > 0) {
        result = String.format("(GMT+%d:%02d) %s", hours, minutes, tz.getID());
    } else {
        result = String.format("(GMT%d:%02d) %s", hours, minutes, tz.getID());
    }

    return result;

}
查看更多
登录 后发表回答