Paul
Paul

Reputation: 97

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

Upvotes: 6

Views: 45488

Answers (9)

Sathiamoorthy
Sathiamoorthy

Reputation: 11580

The below method will be useful generate filename with date time using LocalDateTime using Java 8+

private String getFileName(String fileName) {
    LocalDateTime current = LocalDateTime.now();
    DateTimeFormatter format = DateTimeFormatter.ofPattern("ddMMyyyyHHmmss");
    String name = fileName.substring(0,fileName.lastIndexOf("."));
    String ext = fileName.substring(fileName.lastIndexOf("."));
    return name+"_"+current.format(format)+ext;
}

Upvotes: 1

Vamsi Jayavarapu
Vamsi Jayavarapu

Reputation: 401

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

Upvotes: 2

Akshay Prabhakar
Akshay Prabhakar

Reputation: 11

The below mentioned snippet can be used

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

 logFileName = "loggerFile_" + logFileName;

Upvotes: 0

Kumar Abhishek
Kumar Abhishek

Reputation: 3124

If using java 8

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

Upvotes: 10

jvataman
jvataman

Reputation: 1367

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

Upvotes: 0

Viktor Stolbin
Viktor Stolbin

Reputation: 2939

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

Upvotes: 5

Eric B.
Eric B.

Reputation: 24411

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.

Upvotes: 0

plucury
plucury

Reputation: 1120

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

With the code, You use outFile before initialize it.

Upvotes: 0

Suraj Chandran
Suraj Chandran

Reputation: 24791

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

Basically you are using outFile before its initialized.

Upvotes: 6

Related Questions