gpt4 book ai didi

java-8 - 使用 Java 时间在 Predicate 中将日期时间字符串解析为日期

转载 作者:行者123 更新时间:2023-12-04 07:14:08 25 4
gpt4 key购买 nike

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$0(Program.java:210)
at java.base/java.util.stream.ReferencePipeline$2$1.accept(ReferencePipeline.java:176)
at java.base/java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:195)
at java.base/java.util.stream.SliceOps$1$1.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 .

关于java-8 - 使用 Java 时间在 Predicate 中将日期时间字符串解析为日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68887879/

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