在文件名中添加日期和时间

6

你好,我正在尝试在JAVA中将日期和时间添加到文件名中。我可以将日期和时间打印在文件中,这也是我想要的,但当我将toString放在FileWriter中时,会出现空指针错误。

package com.mkyong;
import java.util.*;
import java.io.*;
import java.*;
import java.util.Date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;

    public class Simplex {

        private static PrintWriter outFile;

        //Main Method
        public static void main(String[] args) throws IOException {



            // Instantiate a Date object
             Date date = new Date();

             // display time and date using toString()
             outFile.println(date.toString());
             outFile.println();
            //creates the new file to be saved


            outFile = new PrintWriter(new FileWriter("simplex" + (date.toString()) + ".txt"));
8个回答

10

如果使用Java 8

DateTimeFormatter timeStampPattern = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");
        System.out.println(timeStampPattern.format(java.time.LocalDateTime.now()));

3
建议始终向 now() 方法传递一个 ZoneId 对象。如果省略不传,则将应用 JVM 当前默认的时区,而该默认值可能会有所不同。最好明确指定期望/所需的时区。 - Basil Bourque

5

outFile = new PrintWriter(..)这一行应该在第一次使用outFile之前出现。

基本上是在使用outFile之前对其进行了初始化。


啊,谢谢。但是我重新排列后,编译器仍然不喜欢这一行代码:outFile = new PrintWriter(new FileWriter("simplex" +(date.toString()) + ".txt")); - Paul
尝试将该行代码放在 date=new Date() 语句之后。 - Suraj Chandran

5

我建议你在文件名中使用 YYYY-MM-dd_hh-mm-ss 格式模式,这样可以更方便地对文件进行排序。请看 SimpleDateFormat 类。

    ...
    Format formatter = new SimpleDateFormat("YYYY-MM-dd_hh-mm-ss");
    outFile = new PrintWriter(new FileWriter("simplex_" + formatter.format(date) + ".txt"))
    ...

2
LocalDateTime current = LocalDateTime.now();
DateTimeFormatter format = DateTimeFormatter.ofPattern("ddMMyyyyHHmmss");
String formatedDateTime = current.format(format);
outFile = new PrintWriter(new FileWriter("simplex" + formatedDateTime  + ".txt"));

0
// display time and date using toString()
outFile.println(date.toString());

使用这段代码时,你在初始化outFile之前就使用了它。


0

只需将其保存在变量中。顺便提一下,您应该使用新的Date(long)构造函数。

public class Simplex {

    private static PrintWriter outFile;

    //Main Method
    public static void main(String[] args) throws IOException {



        // Instantiate a Date object
         Date date = new Date(System.currentTimeMillis());
         String dateString = date.toString();


        outFile = new PrintWriter(new FileWriter("simplex" + dateString + ".txt"));


         outFile.println(dateString);
         outFile.println();
        //creates the new file to be saved

0
问题在于outFile被声明为静态变量,但是在你使用它之前从未初始化。
你需要先初始化/实例化outFile,然后再实际使用它:
 private static PrintWriter outFile;

    //Main Method
    public static void main(String[] args) throws IOException {

        // Instantiate a Date object
         Date date = new Date();

        //creates the new file to be saved
        outFile = new PrintWriter(new FileWriter("simplex" + (date.toString()) + .txt"));
        // display time and date using toString()
         outFile.println(date.toString());
         outFile.println();

虽然我不完全确定为什么你要将outFile创建为静态对象,而不是局部变量。


0

下面提到的代码片段可以使用

 String logFileName = new SimpleDateFormat("yyyyMMddHHmm'.txt'").format(new Date());

 logFileName = "loggerFile_" + logFileName;

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接