我正在编写一个应用程序,让一些线程编写带有随机数字和随机日期的 csv 文件。但是,某些日期输出为 ######,但是当我在 Excell 中单击它们时,它们确实显示正确的日期(例如 11/4/2014 在 Excell 中显示为 ######,但是当我单击此单元格,在 fx 字段中它确实显示为 11/4/2014)。
这是我写日期的方式:
Random random = new Random();
BufferedWriter bufferedWriter;
GregorianCalendar cal = new GregorianCalendar();
DateFormat df = new SimpleDateFormat("dd/MM/yyyy");
writeLine(random, bufferedWriter, cal, df);
private void writeLine(Random random, BufferedWriter bufferedWriter, GregorianCalendar cal, DateFormat df) throws IOException {
String randomDateString;
bufferedWriter.write(Double.toString(random.nextDouble() * 100));
bufferedWriter.write(",");
randomDateString = createRandomDate(random, cal, df);
bufferedWriter.write(randomDateString);
bufferedWriter.newLine();
}
private String createRandomDate(Random random, GregorianCalendar cal, DateFormat df) {
int randomDayOfTheYear;
Date randomDate;
String randomDateString;
randomDayOfTheYear = random.nextInt(365) + 1;
cal.set(Calendar.YEAR, 2014);
cal.set(Calendar.DAY_OF_YEAR, randomDayOfTheYear);
randomDate = cal.getTime();
randomDateString = df.format(randomDate);
return randomDateString;
}
最佳答案
######
是 Excel 用来标记宽度不足以显示其中的整个数字的单元格的一段文本。这种情况只发生在数字上。您正确编写了 CSV 文件。您所要做的就是展开 Excel 中的列,以便它可以显示整个日期。
关于java - 在 csv 文件中写入日期,部分输出为 ######,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21483563/