java - 解析HH :mm:ss from CSV file

标签 java date parsing

我正在加载 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/

相关文章:

mysql - 如何获取日历小时中的日期时间差异

php - 从头开始匹配字符串的一部分

sql-server - 日期转换和文化 : Difference between DATE and DATETIME

android - 带有不可编辑/不可取消后缀的 EditText

java - 通过添加公制距离来获取新的 GPS 坐标

java - 在物理设备上成功启动 Activity 后,应用程序停止 instatnly.NetworkOnMainThreadException

Java访问pci-modem

java - 将包含 bundle 的 bundle 转换为 JSON

php - 在 PHP 中解析 URL 查询

python - 用于获取维基百科编辑内容的 API