在 Java 中将字符串解析为 LocaDateTime

发布于 2025-01-16 10:19:54 字数 361 浏览 0 评论 0原文

考虑一个字符串“2022-03-23 21:06:29.4933333 +00:00”。 如何在 Java 中将上面的 DateTimeOffset 字符串解析为 LocalDateTime?

我尝试使用以下 DateTimeFormatter 但格式似乎不正确:

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss\[.nnnnnnn\] \[+|-\]hh:mm\]");

LocalDateTime dateTime = LocalDateTime.parse(timestamp, formatter)

Consider a String "2022-03-23 21:06:29.4933333 +00:00".
How do I parse the above DateTimeOffset String to LocalDateTime in Java?

I tried with the following DateTimeFormatter but the format seems to be incorrect:

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss\[.nnnnnnn\] \[+|-\]hh:mm\]");

LocalDateTime dateTime = LocalDateTime.parse(timestamp, formatter)

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

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

发布评论

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

评论(2

猫七 2025-01-23 10:19:54

首先,首先使用 JavDocs for DateTimeFormatter 手头的,这将真正帮助确定您需要哪些说明符

首先要做的是将文本解析为ZonedDateTimeLocalDateTime 不会解析带有时区的输入值(AFAIK),您“可能”能够强制它,但有什么意义呢?

String text = "2022-03-23 21:06:29.4933333 +00:00";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm:ss.SSSSSSS z");
ZonedDateTime zdt = ZonedDateTime.parse(text, formatter);
System.out.println(zdt);

这将打印...

2022-03-23T21:06:29.493333300Z

现在您可以使用 ZonedDateTime#toLocalDateTime,但这不会考虑用户/计算机的当前时区。

如果您需要将 ZonedDateTime 转换为 LocalDateTime,最好立即执行此操作,这将转换时间(如果需要,还可以转换日期)以最好地表示当前的时间时区(好吧,我打字很困惑)

例如,将输入值转换为我当前的时区(+11 小时)将如下所示...

ZoneId currentZone = ZoneId.systemDefault();
ZonedDateTime currentZDT = zdt.withZoneSameInstant(currentZone);
System.out.println(currentZDT);
LocalDateTime ldt = currentZDT.toLocalDateTime();
System.out.println(ldt);

它将打印...

2022-03-24T08:06:29.493333300+11:00[Australia/Melbourne]
2022-03-24T08:06:29.493333300

这意味着在晚上 9:06 3月23日格林奇(格林尼治标准时间),那是我住的地方 3 月 24 日上午 8 点 06 分。

现在,您可以使用不同的 ZoneId 转换为 TimeZone,它不是当前计算机 TimeZone,但我将把它留给您进行实验(例如,我使用 将 ZonedDateTime 转换为时区的 LocalDateTime 作为我的示例的基础)

First, start by having the JavDocs for DateTimeFormatter at hand, this is going to really help determine which specifiers you need

The first thing to do is parse the text into a ZonedDateTime, LocalDateTime won't parse a input value with a time zone (AFAIK), you "might" be able to force it, but what's the point?

String text = "2022-03-23 21:06:29.4933333 +00:00";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm:ss.SSSSSSS z");
ZonedDateTime zdt = ZonedDateTime.parse(text, formatter);
System.out.println(zdt);

This prints...

2022-03-23T21:06:29.493333300Z

Now you could use ZonedDateTime#toLocalDateTime, but this won't take into account the current time zone of the user/computer.

If you need to convert the ZonedDateTime to LocalDateTime, it's best to do so in away which will translate the time (and date if required) to best represent the time within the current time zone (okay, I was confused typing it)

For example, converting the input value into my current time zone (+11 hours) would look like this...

ZoneId currentZone = ZoneId.systemDefault();
ZonedDateTime currentZDT = zdt.withZoneSameInstant(currentZone);
System.out.println(currentZDT);
LocalDateTime ldt = currentZDT.toLocalDateTime();
System.out.println(ldt);

which will print...

2022-03-24T08:06:29.493333300+11:00[Australia/Melbourne]
2022-03-24T08:06:29.493333300

This means that at 9:06pm on the 23rd March in Grinch (GMT), it was 8:06am on the 24th March where I live.

Now you can use different ZoneIds to convert to a TimeZone which is not the current computers TimeZone, but I'll leave that up to you to experiment with (for example, I used Convert ZonedDateTime to LocalDateTime at time zone to base my example on)

新人笑 2025-01-23 10:19:54

您需要创建自定义 DateTimeFormatter

import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;

public class Main {
    public static void main(String args[]){
        String dateString = "2022-03-23 21:06:29.4933333 +00:00";

        DateTimeFormatter formatter = new DateTimeFormatterBuilder()
                .append(java.time.format.DateTimeFormatter.ISO_LOCAL_DATE)
                .appendLiteral(' ')
                .append(java.time.format.DateTimeFormatter.ISO_LOCAL_TIME)
                .appendLiteral(' ')
                .appendOffsetId()
                .toFormatter();

        //In case of OffSet matter, retaining the instant
        LocalDateTime localDateTimeSavePointOfTime = OffsetDateTime.parse(dateString, formatter).withOffsetSameInstant(OffsetDateTime.now().getOffset()).toLocalDateTime();

        //In case OffSet does not matter we can skip it
        LocalDateTime localDateTimeSkipOffSet = LocalDateTime.parse(dateString, formatter);

    }
}

You need create custom DateTimeFormatter:

import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;

public class Main {
    public static void main(String args[]){
        String dateString = "2022-03-23 21:06:29.4933333 +00:00";

        DateTimeFormatter formatter = new DateTimeFormatterBuilder()
                .append(java.time.format.DateTimeFormatter.ISO_LOCAL_DATE)
                .appendLiteral(' ')
                .append(java.time.format.DateTimeFormatter.ISO_LOCAL_TIME)
                .appendLiteral(' ')
                .appendOffsetId()
                .toFormatter();

        //In case of OffSet matter, retaining the instant
        LocalDateTime localDateTimeSavePointOfTime = OffsetDateTime.parse(dateString, formatter).withOffsetSameInstant(OffsetDateTime.now().getOffset()).toLocalDateTime();

        //In case OffSet does not matter we can skip it
        LocalDateTime localDateTimeSkipOffSet = LocalDateTime.parse(dateString, formatter);

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