将 GMT+ 日期转换为本地时间(Android)

3

我需要将像 "Mon Nov 14 13:36:01 GMT+03:00 2016" 这样的日期转换为本地时间,因此它必须是 "16:36:01"。

Date serverDate; // Mon Nov 14 13:36:01 GMT+03:00 2016

SimpleDateFormat dateFormat = new SimpleDateFOrmat("hh:mm:ss");
String result = dateFormat.formatDate(serverDate);

结果没有看到GMT +03:00,导致时间错误(13:36而不是16:36)。


2
你所在的本地时区是什么?如果你在GMT+3时区,那么13:36:01就是正确的时间。 - Henry
是的,它已经显示了GMT +3,即日期为13:36。 - aleksandrbel
1个回答

0

所以我只需要创建一个无 ZZZZZ 的格式化程序,并将时区设置为 GMT。

    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss", Locale.getDefault());
    simpleDateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
    final Date localDate = simpleDateFormat.parse("2016-11-11T09:48:24-05:00");

    SimpleDateFormat dateFormat = new SimpleDateFOrmat("hh:mm:ss");
    String result = dateFormat.formatDate(localDate); //12:48:24

执行此代码后,localDate变量的时间将为12:48:24(假设我的时区为GMT +03:00)。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接