[英]How to convert a date a user selects in MM-dd-yyyy format to the format yyyy-MM-dd in Java
[英]How do you set a date in yyyy-MM-dd format and then be able to print it out later on in the same format in Java 1.8?
在以下代碼中:
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
使用默認時間單位和特定時區進行解析的演示:
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]
注意: ZoneId.systemDefault()
返回 JVM 的ZoneId
。 將其替換為適用的ZoneId
例如ZoneId.of("America/New_York")
。 另外,請注意方括號內的可選模式,該模式默認為 0。
從Trail: Date Time 中了解有關現代日期時間 API 的更多信息。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.