我正在加载 CSV 文件,需要将两个字符串解析为本地日期。基本上我的项目是关于高分列表的。
CSV 文件中的示例格式:{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);
当它尝试解析 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
现在我知道这与我之前在 timeFormat 中定义的格式有关,但我仍然没有看到我的错误。希望大家能够帮助我!
最佳答案
我认为你的问题是你尝试将本地时间解析为日期。 在您的情况下,您可以改用 LocalTime 对象。
LocalTime.parse(highscore[3]);
关于java - 解析HH :mm:ss from CSV file,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54447819/