Java 8添加了一个新的java.time API来处理日期和时间(JSR 310).
我有日期和时间作为字符串(例如"2014-04-08 12:30").如何LocalDateTime从给定的字符串中获取实例?
在我完成LocalDateTime对象的使用之后:如何将LocalDateTime实例转换回具有与上面所示格式相同的字符串?
所以我希望这个代码能够在新的Java 8日期/时间包下工作,因为它所做的只是将给定的ZonedDateTime转换为字符串并使用相同的内置DateTimeFormatter实例(ISO_INSTANT)返回:
ZonedDateTime now = ZonedDateTime.now();
System.out.println(ZonedDateTime.parse(
now.format(DateTimeFormatter.ISO_INSTANT),
DateTimeFormatter.ISO_INSTANT));
Run Code Online (Sandbox Code Playgroud)
但显然它没有:
Exception in thread "main" java.time.format.DateTimeParseException: Text '2014-09-01T19:37:48.549Z' could not be parsed: Unable to obtain ZonedDateTime from TemporalAccessor: {MilliOfSecond=549, NanoOfSecond=549000000, MicroOfSecond=549000, InstantSeconds=1409600268},ISO of type java.time.format.Parsed
at java.time.format.DateTimeFormatter.createError(DateTimeFormatter.java:1918)
at java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1853)
at java.time.ZonedDateTime.parse(ZonedDateTime.java:597)
Run Code Online (Sandbox Code Playgroud)
我已经看过这个条目,但它没有帮助我,因为需要ZonedDateTime对象而不是本地对象,还因为我已经安装了8u20:无法使用Java8中的DateTimeFormatter和ZonedDateTime从TemporalAccessor获取ZonedDateTime
任何人都知道这里发生了什么?
我有一个文件可以具有日期修改值,具有日期或日期时间的格式.我曾经将值解析为:
String t = "2012-01-05T21:21:52.834Z";
logger.info(ZonedDateTime.parse(t).toEpochSecond() * 1000);
Run Code Online (Sandbox Code Playgroud)
现在,字符串也可以
t = "2012-01-05";
Run Code Online (Sandbox Code Playgroud)
哪个引起了错误
线程"main"中的异常java.time.format.DateTimeParseException:无法在java.time.format.DateTimeFormatter.parseResolved0(未知来源)的索引10处解析文本"2012-01-05"
如果我用这个字符串(参考)
LocalDate date = LocalDate.parse(t, DateTimeFormatter.ISO_DATE);
logger.info(date.atStartOfDay(ZoneId.of("UTC")).toEpochSecond() * 1000);
Run Code Online (Sandbox Code Playgroud)
这会奏效.但是,正如我已经提到的那样,字符串可以是这些类型中的任何一种,我如何识别格式然后相应地得到毫秒?
在以下代码中:
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 …Run Code Online (Sandbox Code Playgroud)