如何使日期与区域设置无关?
我有一个数据库,以 GMT 时区的 OleDateTime
格式存储日期。我已经实现了一个类,在 java 中扩展了 Date 来以经典日期格式表示它。但我的课程取决于区域设置(我在 GMT+2)。因此,它将数据库中的日期转换为日期 - 2 小时
。如何使其正确转换日期?我希望我的课程与区域设置无关,始终使用 GMT 时区。其实,问题是:
class MyOleDateTime extends Date {
static {
Locale.setDefault(WhatGoesHere?)
}
// ... some constructors
// ... some methods
}
I have a db, that stores dates in OleDateTime
format, in GMT timezone. I've implemented a class, extending Date
in java to represent that in classic date format. But my class is locale-dependent (I'm in GMT+2). Therefore, it converts the date in the db as date - 2 hours
. How do I make it convert the date correctly? I want my class to be locale-independent, always using GMT timezone. Actually, the question is:
class MyOleDateTime extends Date {
static {
Locale.setDefault(WhatGoesHere?)
}
// ... some constructors
// ... some methods
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
好吧,最好像其他答案中建议的那样使用 Calendar 对象。但是,如果您确实想要设置全球时区,则可以在应用程序代码的早期使用
TimeZone.setDefault(TimeZone.getTimeZone("UTC"));
。还有user.timezone
Java 系统属性。另外(很有趣的是),似乎唯一真正采用 GMT/UTC 时间(没有夏令时变化)的国家是利比里亚。
事实上,
Date
对象本身总是与语言环境和时区无关。其getTime()
方法将始终返回自 1970 年 1 月 1 日 00:00:00(不包括闰秒)以来经过的毫秒数(采用 UTC)。但是,如果您想要获取毫秒以外的其他值,则必须使用Calendar
,它取决于时区。但这是正确的方法。您不会在Date
类中使用那些已弃用的方法,对吗?Well, it's better to use the Calendar object like suggested in other answers. However, if you really want to set global timezone, you can use
TimeZone.setDefault(TimeZone.getTimeZone("UTC"));
early in your application code. There is alsouser.timezone
Java system property.Also (just fun to know), it appears that the only country actually living by GMT/UTC time (without daylight saving changes) is Liberia.
In fact,
Date
objects per se are always locale- and timezone-independent. ItsgetTime()
method will always return the number of milliseconds passed since January 1, 1970 00:00:00 (not counting leap seconds) in UTC. But if you want to get something else than milliseconds, you have to useCalendar
, which is timezone-dependent. But it is the right way to go. You don't use that deprecated methods inDate
class, do you?正如 Michael Borgwardt 所说,Java
Date
对象对时区一无所知。它只是自 01-01-1970 00:00:00 UTC 以来的毫秒数的包装。仅当您使用
DateFormat
将Date
对象转换为String
时,您才开始处理时区。您可以在DateFormat
上设置时区,以指定您希望在哪个时区查看Date
。As Michael Borgwardt has already said, the Java
Date
object does not know anything about timezones. It's just a wrapper for a number of milliseconds since 01-01-1970 00:00:00 UTC.You start dealing with timezones only when you for example convert the
Date
object to aString
using aDateFormat
. You set the timezone on theDateFormat
to specify in which timezone you want to see theDate
.日期 与区域设置无关,始终使用 GMT 时区。它只是 GMT(更准确地说:UTC)中毫秒时间戳的包装。
Date
中唯一与时区相关的内容是已弃用的方法,例如getDay()
- 这就是它们被弃用的原因。它们使用默认时区。正确的做法是避免使用那些已弃用的方法 - 不将默认时区设置为 UTC!这可能会在其他地方引起问题,并且您无法阻止代码的其他部分将默认时区设置为其他值。A Date is locale-independent, always using GMT timezone. It's just a wrapper around a millisecond timestamp in GMT (more correctly: UTC).
The only things in
Date
that are timezone dependant are the deprecated methods likegetDay()
- that's why they're deprecated. Those use the default time zone. The correct thing to do is to avoid using those deprecated methods - not to set the default timezone to UTC! That could cause problems elsewhere, and you can't prevent other parts of the code from setting the default timezone to something else.使用 Calendar 对象:
Use a Calendar object:
这是我用来计算与 Calendar 实例的 GMT 偏移量并对其进行格式化的代码片段。我感谢从这个网站获得的所有帮助,很高兴做出贡献。我希望这对某个地方的人有帮助。享受。
Here's a snippet I used to calculate the GMT offset from the
Calendar
instance and format it. I appreciate all the help I've gotten from this site, its nice to contribute. I hope this helps someone somewhere. Enjoy.