将UTC转换为当前的区域设置时间

Lon*_*ick 37 java android utc

我正在从Web服务下载一些JSON数据.在这个JSON中,我有一些日期/时间值.UTC中的所有内容.如何解析此日期字符串,以便结果Date对象在当前语言环境中?

例如:服务器返回"2011-05-18 16:35:01",我的设备现在应显示"2011-05-18 18:35:01"(GMT +2)

我目前的代码:

SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date myDate = simpleDateFormat.parse(rawQuestion.getString("AskDateTime"));

Aff*_*ffe 88

它有一个设定的时区方法:

SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
simpleDateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
Date myDate = simpleDateFormat.parse(rawQuestion.getString("AskDateTime"));
Run Code Online (Sandbox Code Playgroud)

全部完成!

  • 你能解释一下什么是"rawQuestion"和"AskDateTime"? (6认同)

Paw*_*yda 18

所以你想通知UTC时区的SimpleDateFormat:

SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
TimeZone utcZone = TimeZone.getTimeZone("UTC");
simpleDateFormat.setTimeZone(utcZone);
Date myDate = simpleDateFormat.parse(rawQuestion.getString("AskDateTime"));
Run Code Online (Sandbox Code Playgroud)

显示:

simpleDateFormat.setTimeZone(TimeZone.getDefault());
String formattedDate = simpleDateFormat.format(myDate);
Run Code Online (Sandbox Code Playgroud)