2

There is a string field in a CSV file with the following format: 2008-04-11 00:00:00

I need to convert it to date so that I can perform the following calculation in Predicate, to verify date typing errors.

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy");
Predicate<Client> clientBirth = p -> Period.between(LocalDate.parse(p.getBirth().replaceAll("-", "/"), formatter),LocalDate.now()).getYears() >= 100;

But it's giving an error in the first line of the CSV file.

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)

Thanks in advance.

1 Answers1

2
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.S");

use above as your date-time format from CSV is 2008-04-11 00:00:00 not dd/MM/yyyy.

keuleJ
  • 3,418
  • 4
  • 30
  • 51
Vishwa Ratna
  • 5,567
  • 5
  • 33
  • 55
  • Hi Vishwa... Thank you for your reply. I need the format of date from this ==> 2008-04-11 00:00:00 to this ==> 11/04/2008. – Adalberto José Brasaca Aug 23 '21 at 06:11
  • 2
    Du not suggest the old `SimpleDateFormat` – Jens Aug 23 '21 at 06:27
  • 1
    @VishwaRatna I used only the first part ("yyyy-MM-dd") and the calculation worked... Thank you. – Adalberto José Brasaca Aug 23 '21 at 07:55
  • @VishwaRatna Please don’t teach the young ones to use the long outdated and notoriously troublesome `SimpleDateFormat` class. At least not as the first option. And not without any reservation. We have so much better in [`java.time`, the modern Java date and time API,](https://docs.oracle.com/javase/tutorial/datetime/) and its `DateTimeFormatter`. Also your code does nothing to format into a desired format. – Ole V.V. Aug 23 '21 at 18:38