从 Java 中的 CSV 文件中读取和格式化日期

Reading and formatting date from a CSV file in Java

我对 Java 和一般编程还是很陌生。我有一个项目,其中有一个 CSV 文件,必须读取该文件。在 CSV 的第 3 列中,我有一个日期,我需要将其格式设置为 dd/MM/yyyy in Java。然后我需要制作一个对象列表,其中日期是其中一个字段。 使用我在这里找到的类似问题的答案,我写了这段代码:

while ((line = br.readLine()) != null) {
            String[] str = line.split(splitBy);
            SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
            Date dt = sdf.parse(str[2]);
            tripLis.add(new Trip(Integer.parseInt(str[0]), str[1], dt,
                    Integer.parseInt(str[3]), Double.parseDouble(str[4]), str[5]));
        }

但是,这并不能满足我的需要,当我尝试打印列表时,日期的格式类似于“EEE,d MMM yyyy HH:mm:ss Z”。 我应该在我的代码中更改什么?

打印时您可以使用相同的 SimpleDateFormat

Date currentDate = new Date();
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd/MM/yyyy");
System.out.println(simpleDateFormat.format(currentDate));

注意:LocalDate class 正在接管 Date class