从JTable的文本文件中解析日期

时间:2018-06-07 17:28:45

标签: java swing date jtable text-files

我正在尝试解析文本文件http://localhost:5001/version/GetBackgroundId 中的日期:

rents

所以我设置格式06.06.2018.|16.06.2018.|13|Gost|Gostic|617000|false 08.06.2018.|14.06.2018.|12|Guest|Guestic|617000|false

("dd.MM.yyyy.")

但是当我想在JTable中显示它时,显示如下:

enter image description here

但我希望它以与文本文件相同的方式显示。

怎么做?

修改

DateFormat sourceFormat = new SimpleDateFormat("dd.MM.yyyy.");

String startDate = split[0];
Date startDatePars = sourceFormat.parse(startDate);

String endDate = split[1];
Date endDatePars = sourceFormat.parse(endDate);

填充JTable:

static ArrayList<Rent> loadRents() {
        ArrayList<Rent> rents = new ArrayList<Rent>();
        try {
            File rentsFile = new File("src/txt/rents");
            @SuppressWarnings("resource")
            BufferedReader br = new BufferedReader(new FileReader(rentsFile));
            String line = null;
            while ((line = br.readLine()) != null) {
                String[] split = line.split("\\|");
                DateFormat sourceFormat = new SimpleDateFormat("dd.MM.yyyy.");
                String startDate = split[0];
                Date startDatePars = sourceFormat.parse(startDate);
                String endDate = split[1];
                Date endDatePars = sourceFormat.parse(endDate);
                String roomNumber = split[2];
                String guestName = split[3];
                String guestLastname = split[4];
                String guestIDCard = split[5];
                Boolean deleted = Boolean.parseBoolean(split[5]);
                if (deleted)
                    continue;
                Rent newRent = new Rent(startDatePars, endDatePars, roomNumber, guestName, guestLastname, guestIDCard, deleted);
                rents.add(newRent);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return rents;
    }

1 个答案:

答案 0 :(得分:2)

使用LocalDate代替Date并尝试使用此版本,它应该有效...

String content = "06.06.2018.|16.06.2018.|13|Gost|Gostic|617000|false";
String [] split = content.split("\\|");

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd.MM.yyyy.");

LocalDate dateBegin = LocalDate.parse(split[0], formatter);
LocalDate dateEnd = LocalDate.parse(split[1], formatter);

System.out.println(dateBegin); // 2018-06-06
System.out.println(dateEnd); // 2018-06-16

编辑感谢YCF_L提供有关输出格式的提示:

System.out.println(dateBegin.format(formatter)); // 06.06.2018.
System.out.println(dateEnd.format(formatter)); // 16.06.2018.
相关问题