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

use*_*636 2 java date java-8

在以下代码中:

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

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

2021-07-17
Run Code Online (Sandbox Code Playgroud)

但是现在我想将日期更改为不同的日期(例如 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);
Run Code Online (Sandbox Code Playgroud)

但后来我得到以下异常:

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
Run Code Online (Sandbox Code Playgroud)

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

Arv*_*ash 5

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"));
    }
}
Run Code Online (Sandbox Code Playgroud)

输出:

1994-03-24
Run Code Online (Sandbox Code Playgroud)

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);
    }
}
Run Code Online (Sandbox Code Playgroud)

输出:

1994-03-24T00:00Z[Europe/London]
Run Code Online (Sandbox Code Playgroud)

ONLINE DEMO

注意: ZoneId.systemDefault()返回 JVM 的ZoneId. 将其替换为适用的ZoneIdeg ZoneId.of("America/New_York")。另外,请注意方括号内的可选模式,该模式默认为 0。

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