Java 如何使日期与语言环境无关?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2697549/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to make Date locale-independent?
提问by folone
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:
我有一个数据库,它OleDateTime
以格林威治标准时间时区的格式存储日期。我已经实现了一个类,Date
在 java 中扩展以表示经典日期格式。但是我的课程是语言环境相关的(我在 GMT+2)。因此,它将数据库中的日期转换为date - 2 hours
. 如何让它正确转换日期?我希望我的课程与语言环境无关,始终使用 GMT 时区。其实问题是:
class MyOleDateTime extends Date {
static {
Locale.setDefault(WhatGoesHere?)
}
// ... some constructors
// ... some methods
}
采纳答案by Alexander Temerev
Well, it's better to use the Calendar object like suggested in other answers. However, if you reallywant to set global timezone, you can use TimeZone.setDefault(TimeZone.getTimeZone("UTC"));
early in your application code. There is also user.timezone
Java system property.
好吧,最好像其他答案中建议的那样使用 Calendar 对象。但是,如果您确实想设置全局时区,则可以TimeZone.setDefault(TimeZone.getTimeZone("UTC"));
在应用程序代码的早期使用。还有user.timezone
Java系统属性。
Also (just fun to know), it appears that the only country actually living by GMT/UTC time (without daylight saving changes) is Liberia.
另外(知道很有趣),似乎唯一实际生活在 GMT/UTC 时间(没有夏令时更改)的国家是利比里亚。
In fact, Date
objects per se are alwayslocale- and timezone-independent. Its getTime()
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 use Calendar
, which istimezone-dependent. But it is the right way to go. You don't use that deprecated methods in Date
class, do you?
事实上,Date
对象本身总是与语言环境和时区无关的。它的getTime()
方法将始终以 UTC 形式返回自 1970 年 1 月 1 日 00:00:00(不计算闰秒)以来经过的毫秒数。但是,如果你想要得到的东西比别的毫秒,你必须使用Calendar
,这是时区相关。但这是正确的方法。你不会在Date
课堂上使用那些不推荐使用的方法,对吗?
回答by kgiannakakis
回答by Michael Borgwardt
A Dateislocale-independent, always using GMT timezone. It's just a wrapper around a millisecond timestamp in GMT (more correctly: UTC).
一个日期是语言环境无关,总是使用GMT时区。它只是 GMT 中毫秒时间戳的包装器(更准确地说:UTC)。
The only things in Date
that are timezone dependant are the deprecated methods like getDay()
- that's why they're deprecated. Those use the default time zone. The correct thing to do is to avoid using those deprecated methods - notto 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.
唯一Date
与时区相关的是不推荐使用的方法,例如getDay()
- 这就是不推荐使用它们的原因。那些使用默认时区。正确的做法是避免使用那些已弃用的方法 -不要将默认时区设置为 UTC!这可能会导致其他地方出现问题,并且您无法阻止代码的其他部分将默认时区设置为其他内容。
回答by Jesper
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.
正如 Michael Borgwardt 已经说过的,JavaDate
对象对时区一无所知。它只是自 01-01-1970 00:00:00 UTC 以来数毫秒的包装器。
You start dealing with timezones only when you for example convert the Date
object to a String
using a DateFormat
. You set the timezone on the DateFormat
to specify in which timezone you want to see the Date
.
例如,只有当您使用 a将Date
对象转换为 a时,您才开始处理时区。您在 上设置时区以指定要在哪个时区中查看.String
DateFormat
DateFormat
Date
DateFormat df = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss Z");
df.setTimeZone(TimeZone.getTimeZone("UTC"));
String text = df.format(date); // text will contain date represented in UTC
回答by James Ovcarik
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.
这是我用来计算Calendar
实例的 GMT 偏移量并对其进行格式化的片段。我感谢我从这个网站得到的所有帮助,很高兴做出贡献。我希望这可以帮助某个地方的人。享受。
Calendar calInst = Calendar.getInstance();
//calculate the offset to keep calendar instance GMT
int gmtOffsetMilli = calInst.get(Calendar.ZONE_OFFSET);
long gmtOffsetHr = TimeUnit.HOURS.convert(gmtOffsetMilli, TimeUnit.MILLISECONDS);
calInst = Calendar.getInstance(TimeZone.getTimeZone("GMT " + gmtOffsetHr));