FileWriter日期为null值

时间:2013-08-18 02:25:44

标签: java android simpledateformat

我有一个日期值,我想用

写入文件
 fw = new BufferedWriter(new FileWriter(root + "/log.txt"));

             fw.newLine();
             fw.write("," + "server timestamp   " + "," +  " Timestamp");
             fw.newLine();
             fw.write( " ,");
             fw.write(dateString + "," +  currentTimeStamp);

但是每次dateString和currentTimeStamp都是null,通过toast我可以看到dateString和currentTimeStamp不是null,但我不能通过FileWritter写它们。

以下是dateString

的代码
SimpleDateFormat formatter = new SimpleDateFormat("HH-mm-ss_SSS");


             dateString = formatter.format(returnTime);


                 currentTimeStamp = formatter.format(new Date());

3 个答案:

答案 0 :(得分:0)

一个问题是您从未定义returnTime

答案 1 :(得分:0)

变量未正确初始化。

答案 2 :(得分:0)

我正在运行你的代码

String dateString, currentTimeStamp;
SimpleDateFormat formatter = new SimpleDateFormat("HH-mm-ss_SSS");
Date returnTime = new Date();

dateString = formatter.format(returnTime);
currentTimeStamp = formatter.format(new Date());

BufferedWriter fw = new BufferedWriter(new FileWriter("E:\\log.txt"));
fw.newLine();
fw.write("," + "server timestamp   " + "," + " Timestamp");
fw.newLine();
fw.write(" ,");
fw.write(dateString + "," + currentTimeStamp);
fw.flush();
fw.close();

我得到了log.txt

enter image description here