gpt4 book ai didi

java - 如何在 Java 1.8 中设置 yyyy-MM-dd 格式的日期,然后能够以相同的格式打印出来?

转载 作者:行者123 更新时间:2023-12-02 02:06:12 25 4
gpt4 key购买 nike

在下面的代码中:

    ZonedDateTime zdt = ZonedDateTime.now();
DateTimeFormatter FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd");
String zdtString = FORMATTER.format(zdt);
System.out.println(zdtString);

您将看到它以 yyyy-DD-mm 格式打印出当前日期。由于这个问题是在 2021 年 7 月 17 日发布的,因此打印出:

2021-07-17

但现在我想将日期更改为不同的日期(例如 1994-03-24)。

所以我尝试了:

    DateTimeFormatter FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd");
ZonedDateTime zdt2 = ZonedDateTime.parse("1994-03-24", FORMATTER);
String zdtString = FORMATTER.format(zdt2);
System.out.println(zdtString);

但是我得到了以下异常:

Exception in thread "main" java.time.format.DateTimeParseException: Text '1994-03-24' could not be parsed: Unable to obtain ZonedDateTime from TemporalAccessor: {},ISO resolved to 1994-03-24 of type java.time.format.Parsed
at java.time.format.DateTimeFormatter.createError(DateTimeFormatter.java:1920)
at java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1855)
at java.time.ZonedDateTime.parse(ZonedDateTime.java:597)
at javaapplication5.JavaApplication5.main(JavaApplication5.java:48)
Caused by: java.time.DateTimeException: Unable to obtain ZonedDateTime from TemporalAccessor: {},ISO resolved to 1994-03-24 of type java.time.format.Parsed
at java.time.ZonedDateTime.from(ZonedDateTime.java:565)
at java.time.format.Parsed.query(Parsed.java:226)
at java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1851)
... 2 more
Caused by: java.time.DateTimeException: Unable to obtain ZoneId from TemporalAccessor: {},ISO resolved to 1994-03-24 of type java.time.format.Parsed
at java.time.ZoneId.from(ZoneId.java:466)
at java.time.ZonedDateTime.from(ZonedDateTime.java:553)
... 4 more

如何将自己的日期设置为当前日期以外的日期?

最佳答案

1994-03-24 没有时区信息,因此在您提供时区信息之前无法将其解析为 ZonedDateTime。此外,您还需要默认时间单位。

1994-03-24 可以直接解析为 LocalDate,因为现代日期时间 API 基于 ISO 8601并且不需要显式使用 DateTimeFormatter 对象,只要日期时间字符串符合 ISO 8601 标准即可。

import java.time.LocalDate;

public class Main {
public static void main(String[] args) {
System.out.println(LocalDate.parse("1994-03-24"));
}
}

输出:

1994-03-24

ONLINE DEMO

使用默认时间单位和特定时区进行解析的演示:

import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
import java.time.temporal.ChronoField;
import java.util.Locale;

public class Main {
public static void main(String[] args) {
DateTimeFormatter dtf = new DateTimeFormatterBuilder()
.appendPattern("u-M-d[ H]")
.parseDefaulting(ChronoField.HOUR_OF_DAY, 0)
.toFormatter(Locale.ENGLISH)
.withZone(ZoneId.systemDefault());


ZonedDateTime zdt = ZonedDateTime.parse("1994-03-24", dtf);
System.out.println(zdt);
}
}

输出:

1994-03-24T00:00Z[Europe/London]

ONLINE DEMO

注意: ZoneId.systemDefault() 返回 JVM 的 ZoneId。将其替换为适用的 ZoneId 例如ZoneId.of(“美国/纽约”)。另外,请注意方括号内的可选模式,默认为 0。

Trail: Date Time 了解有关现代日期时间 API 的更多信息

关于java - 如何在 Java 1.8 中设置 yyyy-MM-dd 格式的日期,然后能够以相同的格式打印出来?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68424630/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com