如何使日期与区域设置无关?

发布于 2024-08-30 11:43:31 字数 380 浏览 9 评论 0原文

我有一个数据库,以 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

維他命╮ 2024-09-06 11:43:31

好吧,最好像其他答案中建议的那样使用 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 also user.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. 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 is timezone-dependent. But it is the right way to go. You don't use that deprecated methods in Date class, do you?

笔芯 2024-09-06 11:43:31

正如 Michael Borgwardt 所说,Java Date 对象对时区一无所知。它只是自 01-01-1970 00:00:00 UTC 以来的毫秒数的包装。

仅当您使用 DateFormatDate 对象转换为 String 时,您才开始处理时区。您可以在 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

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 a String using a DateFormat. You set the timezone on the DateFormat to specify in which timezone you want to see the 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
将军与妓 2024-09-06 11:43:31

日期 与区域设置无关,始终使用 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 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 - 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.

苏别ゝ 2024-09-06 11:43:31

使用 Calendar 对象:

Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("GMT"),
                                    locale);

Use a Calendar object:

Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("GMT"),
                                    locale);
咋地 2024-09-06 11:43:31

这是我用来计算与 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));

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 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));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文