使用 Java 时间将日期时间字符串解析为 Predicate 中的日期
Parsing a datetime string to date in Predicate using Java Time
CSV 文件中有一个字符串字段,格式如下:2008-04-11 00:00:00
我需要将其转换为日期,以便我可以在 Predicate 中执行以下计算,以验证日期输入错误。
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy");
Predicate<Client> clientBirth = p -> Period.between(LocalDate.parse(p.getBirth().replaceAll("-", "/"), formatter),LocalDate.now()).getYears() >= 100;
但是它在 CSV 文件的第一行给出了一个错误。
Exception in thread "main" java.time.format.DateTimeParseException: Text '2008/04/11 00:00:00' could not be parsed at index 2
at java.base/java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:2046)
at java.base/java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1948)
at java.base/java.time.LocalDate.parse(LocalDate.java:428)
at application.Program.lambda[=12=](Program.java:210)
at java.base/java.util.stream.ReferencePipeline.accept(ReferencePipeline.java:176)
at java.base/java.util.stream.ReferencePipeline.accept(ReferencePipeline.java:195)
at java.base/java.util.stream.SliceOps.accept(SliceOps.java:199)
at java.base/java.nio.file.FileChannelLinesSpliterator.forEachRemaining(FileChannelLinesSpliterator.java:114)
at java.base/java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:484)
at java.base/java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:474)
at java.base/java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:913)
at java.base/java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
at java.base/java.util.stream.ReferencePipeline.collect(ReferencePipeline.java:578)
at services.CsvProcessingService.loadBirthError(CsvProcessingService.java:123)
at services.CsvProcessingService.processBirthError(CsvProcessingService.java:109)
at application.Program.main(Program.java:214)
提前致谢。
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.S");
使用上面的 CSV 日期时间格式是 2008-04-11 00:00:00
而不是 dd/MM/yyyy
。
CSV 文件中有一个字符串字段,格式如下:2008-04-11 00:00:00
我需要将其转换为日期,以便我可以在 Predicate 中执行以下计算,以验证日期输入错误。
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy");
Predicate<Client> clientBirth = p -> Period.between(LocalDate.parse(p.getBirth().replaceAll("-", "/"), formatter),LocalDate.now()).getYears() >= 100;
但是它在 CSV 文件的第一行给出了一个错误。
Exception in thread "main" java.time.format.DateTimeParseException: Text '2008/04/11 00:00:00' could not be parsed at index 2
at java.base/java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:2046)
at java.base/java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1948)
at java.base/java.time.LocalDate.parse(LocalDate.java:428)
at application.Program.lambda[=12=](Program.java:210)
at java.base/java.util.stream.ReferencePipeline.accept(ReferencePipeline.java:176)
at java.base/java.util.stream.ReferencePipeline.accept(ReferencePipeline.java:195)
at java.base/java.util.stream.SliceOps.accept(SliceOps.java:199)
at java.base/java.nio.file.FileChannelLinesSpliterator.forEachRemaining(FileChannelLinesSpliterator.java:114)
at java.base/java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:484)
at java.base/java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:474)
at java.base/java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:913)
at java.base/java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
at java.base/java.util.stream.ReferencePipeline.collect(ReferencePipeline.java:578)
at services.CsvProcessingService.loadBirthError(CsvProcessingService.java:123)
at services.CsvProcessingService.processBirthError(CsvProcessingService.java:109)
at application.Program.main(Program.java:214)
提前致谢。
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.S");
使用上面的 CSV 日期时间格式是 2008-04-11 00:00:00
而不是 dd/MM/yyyy
。