Android 中的 UTC 日期时间到设备本地日期时间转换[已关闭]

问题描述 投票:0回答:1

我是 Android 开发新手。我收到了 UTC 日期时间,例如“2014-03-03T01:02:19+00:00”。我想将其转换为 android 中的设备本地时区。我看到一些关于它的帖子,但无法理解我应该将该代码放在哪里。任何帮助,将不胜感激。预先感谢。

android timezone utc
1个回答
4
投票

试试这个:

我在我的代码中使用了这个

public  String getDateCurrentTimeZone(long timestamp) {
        try{
            Calendar calendar = Calendar.getInstance();
            TimeZone tz = TimeZone.getDefault();
            calendar.setTimeInMillis(timestamp * 1000);
            calendar.add(Calendar.MILLISECOND, tz.getOffset(calendar.getTimeInMillis()));
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date currenTimeZone = (Date) calendar.getTime();
            return sdf.format(currenTimeZone);
        }catch (Exception e) {
        }
        return "";
    }
© www.soinside.com 2019 - 2024. All rights reserved.