如何使date.getTime()返回UTC时间? [英] How to make date.getTime() returns UTC time?

查看:1016
本文介绍了如何使date.getTime()返回UTC时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Date对象,表示UTC时间。当我使用getTime()方法来获取这个对象的long值时,返回的值对应于我们的本地时间(美国中部)。



谢谢

什么是正确的方法来获取对应于原始UTC时间的值?解决方案

DateFormat 类有一种设置首选时区的方法,并且有一个时区类具有 UTC时间的设置



所以,例如,

  SimpleDateFormat sdf = new SimpleDateFormat(); 
sdf.setTimeZone(new SimpleTimeZone(SimpleTimeZone.UTC_TIME,UTC));
Date yourUtcDate = sdf.parse(yourOriginalDate);


I have a Date object which represents a UTC time. When I use the method getTime() to get the long value of this object, the value returned corresponds to our local time (central US). What is the correct way to get the value back which corresponds to the original UTC time?

Thanks

解决方案

The DateFormat class has a method for setting your preferred time zone, and there's a time zone class that has a setting for UTC time.

So, for example,

SimpleDateFormat sdf = new SimpleDateFormat();
sdf.setTimeZone(new SimpleTimeZone(SimpleTimeZone.UTC_TIME, "UTC"));
Date yourUtcDate = sdf.parse(yourOriginalDate);

这篇关于如何使date.getTime()返回UTC时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆