毫秒时间在格林尼治标准时间在Java中 [英] Milliseconds to Date in GMT in Java
问题描述
我要隐蔽毫秒为GMT日期(Android应用程序),例如:
I need to covert milliseconds to GMT date (in Android app), example:
1372916493000
当我将它转换这个code:
When I convert it by this code:
Calendar cal = Calendar.getInstance();
cal.setTimeZone(TimeZone.getTimeZone("GMT"));
cal.setTimeInMillis(millis);
Date date = cal.getTime();
的结果是 07:41 2013年7月4日
。其结果是一样的,当我只使用:
the result is 07:41 07/04/2013
. The result is the same when I use just:
Date date = new Date(millis);
不幸的是,结果看起来不正确,它看起来像我的本地时间。我试图通过此服务,其结果是 05:41 2013年7月4日
,我相信这是正确的。所以,我两个小时的差别。任何人有任何建议/提示有什么不对我的转换?
Unfortunately the result looks incorrect, it looks like my local time. I tried to convert the same number by this service and the result is 05:41 07/04/2013
, which I believe is correct. So I have two hours difference. Anybody has any suggestion / tips what's wrong with my conversion?
推荐答案
如果结果看起来不正确的手段的System.out.println(日期)
则一点也不奇怪,因为与Date.toString
转换日期为字符串重新presentation在当地时区。要了解导致格林尼治标准时间,你可以使用这个
If result which looks incorrect means System.out.println(date)
then it's no surprise, because Date.toString
converts date into string representation in local timezone. To see result in GMT you can use this
SimpleDateFormat df = new SimpleDateFormat("hh:ss MM/dd/yyyy");
df.setTimeZone(TimeZone.getTimeZone("GMT"));
String result = df.format(millis);
这篇关于毫秒时间在格林尼治标准时间在Java中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!