使用java split从文件中解析日期

问题描述 投票:0回答:1

我有解析文件的代码,然后将行插入表中。我最初忽略了第一行,因为它只有日期和无用的数据。我现在想获取日期并将其插入到表中的所有行中,但不知道该怎么做。我知道我将需要编辑我的实体以包括日期,但不知道如何编辑我的代码以解析日期,或者如果最好有一个仅插入日期的新方法,则更好。

文件格式

20200310|extra|extra|extra||
Mn1223|01192|windows|extra|extra|extra||
Sd1223|02390|linux|extra|extra|extra||
2

当前表

account_name      command_name   system_name    createDt
Mn1223            01192          windows        NULL
Sd1223            02390          linux          NULL

我想将20200310插入createDt

实体/模型

public ZygateEntity(String accountName, String commandName, String systemName){
        this.accountName=accountName;
        this.commandName=commandName;
        this.systemName=systemName;
    }

   //getters and setters

解析文件方法

   private List<ZygateEntity> parseZygateData() throws IOException {
        String filePath = "C:\\DEV\\Test_file.xlsx";

        List<String> lines = Files.readAllLines(Paths.get(filePath));

        // remove date and amount
        lines.remove(0);
        lines.remove(lines.size() - 1);

        return lines.stream()
                .map(s -> s.split("[|]")).map(val -> new ZygateEntity(val[0],val[1],val[2])).collect(Collectors.toList());
    }

插入方法

public void insertZygateData(List<ZygateEntity> parseData) {
    String sql = "INSERT INTO Landing.midrange_xygate_load (account_name,command_name,system_name)"+
            "VALUES (:account_name,:command_name,:system_name)";

    for (ZygateEntity zygateInfo : parseData){
        SqlParameterSource source = new MapSqlParameterSource("account_name", zygateInfo.getAccountName())
                .addValue("command_name", zygateInfo.getCommandName())
                .addValue("system_name", zygateInfo.getSystemName());
        namedParameterJdbcTemplate.update(sql, source);
    }
}

我有解析文件的代码,然后将行插入表中。我最初忽略了第一行,因为它只有日期和无用的数据。我现在想获取日期并将其插入所有...

java spring-jdbc jdbctemplate delimited-text file.readalllines
1个回答
0
投票

java.time

© www.soinside.com 2019 - 2024. All rights reserved.