将 W3C 时区转换为设备时区 android

Convert W3C TimeZone to device TimeZone android

我有什么

我有一个来自服务器的日期字符串,它是 w3c 日期格式 2016-02-13T09:53:49.871Z

我的问题

当我 post 向服务器发送消息时,它显示 5 小时前而不是现在

我想要的

我希望将服务器 TimeZone 转换为设备 TimeZone,这样无论在哪个地区,用户都将看到他的本地时间格式

希望这个 blogspot 和 link 你所期待的:

http://javagrip.blogspot.in/2012/09/date-from-one-timezone-to-another-in-java.html

How to convert UTC timestamp to device local time in android

我解决了,有需要的可以参考这段代码

public String convertW3CTODeviceTimeZone(String strDate) throws Exception {
        SimpleDateFormat simpleDateFormatW3C = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", Locale.US);
        simpleDateFormatW3C.setTimeZone(TimeZone.getTimeZone("GMT"));
        Date dateServer = simpleDateFormatW3C.parse(strDate);

        TimeZone deviceTimeZone = TimeZone.getDefault();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        simpleDateFormat.setTimeZone(deviceTimeZone);

        String formattedDate = simpleDateFormat.format(dateServer);
        return formattedDate;
    }