如何在 Joda-Time 时间戳中保留时区?

发布于 2024-11-06 05:14:39 字数 604 浏览 3 评论 0 原文

我正在解析时间戳。当我读入它们时,它们被强制为我的本地时区(欧洲/伦敦)。我想保留原始时区偏移量。

scala> val fmt = org.joda.time.format.ISODateTimeFormat.dateTimeNoMillis()

scala> val t = fmt parseDateTime ("2012-04-16T23:00:45-04:00")
t: org.joda.time.DateTime = 2012-04-17T04:00:45.000+01:00

scala> t.getDayOfMonth
res2: Int = 17

scala> fmt print t
res1: java.lang.String = 2012-04-17T04:00:45+01:00

在此示例中,从 America/New_York 的时间戳被强制发送到 Europe/London。当我将 DateTime 转换回字符串时,我想取回我输入的原始字符串。

此外,当我询问时间戳是从一个月的哪一天开始时,我希望它说它是从 16 日开始的(因为这就是日期位于生成日期的位置),而不是 17 日(即使那是同一时刻我所在时区的日期)。

我该怎么做?

I'm parsing timestamps. They are forced to my local time zone (Europe/London) when I read them in. I want to preserve the original time zone offset instead.

scala> val fmt = org.joda.time.format.ISODateTimeFormat.dateTimeNoMillis()

scala> val t = fmt parseDateTime ("2012-04-16T23:00:45-04:00")
t: org.joda.time.DateTime = 2012-04-17T04:00:45.000+01:00

scala> t.getDayOfMonth
res2: Int = 17

scala> fmt print t
res1: java.lang.String = 2012-04-17T04:00:45+01:00

In this example, a time stamp from America/New_York is forced to Europe/London. When I convert the DateTime back to a String, I want to get back the original string I fed in.

Additionally, when I ask the timestamp what day of the month it's from, I want it to say it's from the 16th (because that's what the date was in the place it was generated), not the 17th (even though that's what the date was in my time zone at the same instant).

How do I do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

祁梦 2024-11-13 05:14:39

尝试创建一个 DateTimeFormatter 这应该会导致解析的 DateTime 对象保留最初解析的字符串的偏移量。

如果这不起作用,您可能需要单独存储时区。然后,要打印日期,您需要检索日期本身以及目标时区。 设置格式化程序与所需的时区,并用它来格式化日期。

Try creating a DateTimeFormatter with offset parsing enabled. This should cause parsed DateTime objects to retain the offset from the string that was originally parsed.

If that doesn't work, you may need to store the time zone separately. Then, to print a date, you retrieve the date itself, and the target time zone. Set the formatter with the desired time zone, and use it to format the date.

清眉祭 2024-11-13 05:14:39

java.time

下面显示的是 Joda-Time 主页上的通知< /a>:

请注意,从 Java SE 8 开始,要求用户迁移到 java.time (JSR-310) - JDK 的核心部分,取代这个
项目。

使用现代日期时间 API 的解决方案

您不需要使用 java.time API 启用偏移量解析,这是 OffsetDateTime#parse。将日期时间字符串解析为 OffsetDateTime 后,如果需要,可以将其转换为另一个具有不同时区偏移量的 OffsetDateTime

演示:

public class Main {
    public static void main(String[] args) {
        OffsetDateTime odt = OffsetDateTime.parse("2012-04-16T23:00:45-04:00");
        System.out.println(odt);

        // If you want to convert it into another `OffsetDateTime` with a different
        // time zone offset e.g. at ZoneOffset.of("+01:00")
        System.out.println(odt.withOffsetSameInstant(ZoneOffset.of("+01:00")));
    }
}

输出:

2012-04-16T23:00:45-04:00
2012-04-17T04:00:45+01:00

在线演示

/ /编辑

值得一提的是https://stackoverflow.com/users/5772882/anonymous:

对于转换到不同的区域,我相信对于绝大多数人来说
您想要指定区域而不是偏移量的目的,所以
类似于 odt.atZoneSameInstant(ZoneId.of("Europe/London")) 。或者
如果需要 OffsetDateTime
odt.atZoneSameInstant(ZoneId.of("欧洲/伦敦")).toOffsetDateTime()

跟踪:日期时间

java.time

Shown below is a notice on the Joda-Time Home Page:

Note that from Java SE 8 onwards, users are asked to migrate to java.time (JSR-310) - a core part of the JDK which replaces this
project.

Solution using modern date-time API

You do not need to enable offset parsing using the java.time API, it is the default behaviour of OffsetDateTime#parse. Once you have parsed your date-time string into an OffsetDateTime, you can convert it another OffsetDateTime with a different time zone offset if you need one.

Demo:

public class Main {
    public static void main(String[] args) {
        OffsetDateTime odt = OffsetDateTime.parse("2012-04-16T23:00:45-04:00");
        System.out.println(odt);

        // If you want to convert it into another `OffsetDateTime` with a different
        // time zone offset e.g. at ZoneOffset.of("+01:00")
        System.out.println(odt.withOffsetSameInstant(ZoneOffset.of("+01:00")));
    }
}

Output:

2012-04-16T23:00:45-04:00
2012-04-17T04:00:45+01:00

Online Demo

// EDIT

It is worth mentioning the following comment by https://stackoverflow.com/users/5772882/anonymous:

For converting to a different zone I believe the for the vast majority
of purposes you would want to specify the zone, not the offset, so
something like odt.atZoneSameInstant(ZoneId.of("Europe/London")). Or
if an OffsetDateTime would be needed,
odt.atZoneSameInstant(ZoneId.of("Europe/London")).toOffsetDateTime()

Learn more about the modern Date-Time API from Trail: Date Time.

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