我使用 DateTimeZone.convertLocalToUTC 将本地时间转换为 UTC。时间更改正确,但转换后,时区信息仍显示原始本地时区。请引用下面的示例代码
Date gmttime = new Date(tz.convertLocalToUTC(System.currentTimeMillis(),false));
System.out.println(gmttime.toString());
输出: 10 月 16 日星期三 12:58:19 IST 2013
请注意粗体值,我预计它是 UTC 。如果我遗漏了什么,请告诉我。
最佳答案
#Date.toString()
将以本地时区打印日期。
使用 SimpleDateFormat
打印针对特定 TimeZone
格式化的 Date
:
public static void main(String[] args) {
Date date = new Date();
SimpleDateFormat format = new SimpleDateFormat("E MMM dd HH:mm:ss:SS z");
format.setTimeZone(TimeZone.getTimeZone("UTC"));
System.out.println(format.format(date));
}
关于java - 在 DateTimeZone.convertLocalToUTC 时区仍然显示本地,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19398719/