代码之家  ›  专栏  ›  技术社区  ›  Paresh Mayani jeet

根据当前时区将UTC日期/时间显示为日期/时间

  •  7
  • Paresh Mayani jeet  · 技术社区  · 14 年前

    我正在从Web上获取一个日期/时间字符串,格式为“yyyy/mm/dd't'hh:mm:ss'z'”,格式为UTC。

    现在我必须确定设备的当前时区,然后将此时间转换为本地时间。

    我该怎么做,请建议我!!

    (仅供参考,目前,UTC时间为上午10:25,在印度,当前时间为下午3:55)

    1 回复  |  直到 14 年前
        1
  •  13
  •   Lie Ryan Bryan    14 年前

    TimeZone.getDefault() TimeZone.getTimeZone("GMT")

    the docs

    SimpleDateFormat

     // note that I modified the format string slightly
     SimpleDateFormat fmt = new SimpleDateFormat("yyyy/MM/dd'T'HH:mm:ss'Z'");
     // set the timezone to the original date string's timezone
     fmt.setTimeZone(TimeZone.getTimeZone("GMT"));
     Date date = fmt.parse("1998/12/21T13:29:31Z", new ParsePosition(0));
    
     // then reset to the target date string's (local) timezone
     fmt.setTimeZone(TimeZone.getDefault());
     String localTime = fmt.format(date);