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!

3
  • 1
    Don't you want to use LocalTime when parsing the time column? Commented Jan 30, 2019 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 Commented Jan 30, 2019 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). Commented Jan 30, 2019 at 19:33

1 Answer 1

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]); 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.