0

I'm loading a CSV file and need to parse two strings to localdate. Basically my project is about a Highscore list.

Example format in CSV file: {David,18.01.2019,Easy,00:05:30}

br = new BufferedReader(new FileReader(csvFile));
DateTimeFormatter dateFormat = DateTimeFormatter.ofPattern("dd.MM.yyyy");
DateTimeFormatter timeFormat = DateTimeFormatter.ofPattern("HH:mm:ss");

while ((line = br.readLine()) != null) {
    String[] highscore = line.split(cvsSplitBy);

    HighScore highScore = new HighScore(
        highscore[0],
        LocalDate.parse(highscore[1], dateFormat),
        highscore[2],
        LocalDate.parse(highscore[3], timeFormat));
    highScoreList.add(highScore);

It throws an exception when it tries to parse 00:05:30.

java.time.format.DateTimeParseException: Text '00:05:30' could not be parsed: Unable to obtain LocalDate from TemporalAccessor: {},ISO resolved to 00:05:30 of type java.time.format.Parsed

Now I know this has something to do with the format I previously defined in timeFormat but I still don't see my mistake. I hope you guys can help me!

Sean Bright
  • 118,630
  • 17
  • 138
  • 146
Vins
  • 291
  • 1
  • 5
  • 19
  • 1
    Don't you want to use `LocalTime` when parsing the time column? – Sean Bright Jan 30 '19 at 19:08
  • Didn't know that LocalTime exists. It works now thank you! Feel kind of dumb right now but I guess that's just normal for an inexperienced Java developer – Vins Jan 30 '19 at 19:29
  • 1
    00:05:30, is that a duration, an amount of time? IN that case you need the `Duration` class (neither `LocalDate` nor `LocalTime`). – Ole V.V. Jan 30 '19 at 19:33

1 Answers1

2

I think your problem is that you try to parse local time into a date. In your case,you can use LocalTime object instead.

LocalTime.parse(highscore[3]);
Tomarlin
  • 63
  • 7