日历不返回 GMT
我试图将日历对象设置为 GMT,但 getTime() 始终返回 GMT+1 时间(我当前的时间)。我尝试过:
Calendar cal = new GregorianCalendar(TimeZone.getTimeZone("GMT"));
Calendar cal = new GregorianCalendar(TimeZone.getTimeZone("UTC"));
Calendar cal = new GregorianCalendar(TimeZone.getTimeZone("skeniver"));
它们显然都返回 GMT,因为
cal.getTimeZone().getDisplayName()
返回“GMT+00:00”;但
cal.getTime().toString();
始终显示 GMT+1 时间。
有谁知道为什么会发生这种情况?
I am trying to get a calendar object set to GMT, but the getTime() always returns the time in GMT+1 (my current time). I have tried:
Calendar cal = new GregorianCalendar(TimeZone.getTimeZone("GMT"));
Calendar cal = new GregorianCalendar(TimeZone.getTimeZone("UTC"));
Calendar cal = new GregorianCalendar(TimeZone.getTimeZone("skeniver"));
They all apparently return GMT, because
cal.getTimeZone().getDisplayName()
returns "GMT+00:00"; but
cal.getTime().toString();
always displays the time in GMT+1.
Does anyone have any idea why this is happening?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您需要根据夏令时进行调整。我不确定这是否有帮助,但它是我用来在一个应用程序中将任何时区调整为 UTC 的代码,该应用程序目前已被世界各地的许多人使用。我使用
Date
而不是Calendar
但它有效......You need to adjust for daylight savings. I'm not sure if this will help but it's code I use for adjusting any timezone to UTC in an app that's currently being used by a number of people around the world. I use
Date
instead ofCalendar
but it works...如果您想使用字符串,那么更喜欢 DateFormat 或 SimpleDateFormat,这里
是示例
If you want to in string then prefer the DateFormat or SimpleDateFormat for this
here is example
Calendar.getTime()
返回一个Date
对象。在 Java 中,Date
只是一个从 UNIX 纪元开始的长时间戳的持有者。要在不同于默认时区的
TimeZone
中显示Date
,您可以使用SimpleDateFormat
。Calendar.getTime()
returns aDate
object. In Java, aDate
is just a holder to a long timestamp starting in the UNIX epoch.To display a
Date
in a differentTimeZone
than the default, you can use aSimpleDateFormat
.