Reputation: 97
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
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
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
Reputation: 11
The below mentioned snippet can be used
String logFileName = new SimpleDateFormat("yyyyMMddHHmm'.txt'").format(new Date());
logFileName = "loggerFile_" + logFileName;
Upvotes: 0
Reputation: 3124
If using java 8
DateTimeFormatter timeStampPattern = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");
System.out.println(timeStampPattern.format(java.time.LocalDateTime.now()));
Upvotes: 10
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
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
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
Reputation: 1120
// display time and date using toString()
outFile.println(date.toString());
With the code, You use outFile
before initialize it.
Upvotes: 0
Reputation: 24791
The line outFile = new PrintWriter(..)
should occur before first usage of outFile.
Basically you are using outFile before its initialized.
Upvotes: 6