有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java解析CSV文件中的HH:mm:ss

我正在加载一个CSV文件,需要将两个字符串解析为localdate。基本上我的项目是关于高分列表的

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中定义的格式有关,但我仍然没有看到我的错误。我希望你们能帮我


共 (1) 个答案

  1. # 1 楼答案

    我认为你的问题在于你试图把当地时间解析成一个日期。 在您的情况下,可以使用LocalTime对象

    LocalTime.parse(highscore[3]);