在 java 中读取 csv 时日期格式自动更改

Date format auto changes while reading csv in java

我正在通过 line.In 读取 csv 行 csv 日期格式适用于 eg-27/04/2015 但我通过我的 java 代码读取了同一行,它读取为 27-04-2015 . 有人可以提出同样的建议吗? 下面是我的代码-

try {
            br = new BufferedReader(new FileReader(fileName));
            while ((line = br.readLine()) != null) {
                temp = line;// Simply reading it not using any date format.

                System.out.println("line="+line);

            }

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

下面是这行,我正在尝试阅读

1/27/2015,B11100054,US_EOD,20141001_00_00_00,20141001_22_26_25,148.50915,147.68575

一旦您按照自己的方式阅读每个 line,就可以通过分隔符值将其拆分。

String[] values = line.split(",");  

// if input format is "MM/dd/yyyy", you can read date: 
DateFormat df = new SimpleDateFormat("MM/dd/yyyy"); 
Date dateInput = df.parse(values[0]); // Here you have date stored

记住 dateInputjava.util.Date 的对象,因此它 没有格式 。这只是一个约会。
如果你需要一个包含 dateInput 的其他格式的字符串,只需使用 SimpleDateFormat

格式化它
df = new SimpleDateFormat("dd-MM-yyyy"); 
String dateStr = df.format(date); 

然后您可以遍历所有其他值:

for (int i = 1; i < values.length; i++) { // Start from 1 because 0, date, is already read. 
    // read values[i], depending on its contents, or keep them as String
}