繁体   English   中英

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

[英]Convert W3C TimeZone to device TimeZone android

我拥有的

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

我的问题

当我将消息发布到服务器时,它显示 5 小时前而不是现在

我想要的是

我希望将服务器TimeZone转换为设备TimeZone ,以便无论区域如何,用户都会看到他的本地时间格式

我解决了,如果有人需要可以参考这个代码

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;
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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