6

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"));
Paul
  • 97
  • 1
  • 1
  • 3

8 Answers8

10

If using java 8

DateTimeFormatter timeStampPattern = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");
        System.out.println(timeStampPattern.format(java.time.LocalDateTime.now()));
Kumar Abhishek
  • 3,004
  • 33
  • 29
  • 3
    I suggest always passing a `ZoneId` object to the `now()` method. If omitted, the JVM‘s current default time zone is applied. That default can vary. Better to specify explicitly the expected/desired zone. – Basil Bourque Apr 18 '16 at 19:42
5

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

Basically you are using outFile before its initialized.

Suraj Chandran
  • 24,433
  • 12
  • 63
  • 94
  • Ah Thank you. But when I reorder it, the compiler still does not like this line: outFile = new PrintWriter(new FileWriter("simplex" +(date.toString()) + ".txt")); – Paul Jun 18 '12 at 17:51
  • Try putting that line just after the date=new Date() statement – Suraj Chandran Jun 18 '12 at 17:57
5

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"))
    ...
Viktor Stolbin
  • 2,899
  • 4
  • 32
  • 53
2
LocalDateTime current = LocalDateTime.now();
DateTimeFormatter format = DateTimeFormatter.ofPattern("ddMMyyyyHHmmss");
String formatedDateTime = current.format(format);
outFile = new PrintWriter(new FileWriter("simplex" + formatedDateTime  + ".txt"));
Vamsi Jayavarapu
  • 105
  • 2
  • 11
0
// display time and date using toString()
outFile.println(date.toString());

With the code, You use outFile before initialize it.

plucury
  • 1,120
  • 1
  • 8
  • 16
0

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
YvesLeBorg
  • 9,070
  • 8
  • 35
  • 48
jvataman
  • 1,357
  • 1
  • 12
  • 13
0

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.

Eric B.
  • 23,425
  • 50
  • 169
  • 316
0

The below mentioned snippet can be used

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

 logFileName = "loggerFile_" + logFileName;