'Adding The Date and Time to the File name

Hello I am trying to add the date and time to a file name in JAVA. I can get the date and time printed within the file, which I also want done, but when I place the toString in the FileWriter I get a Null Pointer.

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"));


Solution 1:[1]

If using java 8

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

Solution 2:[2]

The line outFile = new PrintWriter(..) should occur before first usage of outFile.

Basically you are using outFile before its initialized.

Solution 3:[3]

I'd suggest you to use YYYY-MM-dd_hh-mm-ss formatting pattern in file name that allows you to sort out files in a more convinient way. Take a look at SimpleDateFormat class.

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

Solution 4:[4]

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

Solution 5:[5]

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

With the code, You use outFile before initialize it.

Solution 6:[6]

Problem is that outFile is declared as a static, but never initialized until after you already used it.

You need to first initialize/instantiate outFile first before actually using it:

 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();

Although I'm not entirely sure why you are even creating outFile as a static object, and not just a local variable.

Solution 7:[7]

Just save it in a variable. You should use the new Date(long) constructor, btw.

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

Solution 8:[8]

The below mentioned snippet can be used

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

 logFileName = "loggerFile_" + logFileName;

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Kumar Abhishek
Solution 2 Suraj Chandran
Solution 3
Solution 4 Vamsi Jayavarapu
Solution 5 plucury
Solution 6 Eric B.
Solution 7 YvesLeBorg
Solution 8 Akshay Prabhakar