Current timestamp as filename in Java

JavaTimestamp

Java Problem Overview


I want to name new files created by my Java application with the current timestamp.

I need help with this. How do I name the new files created with the current timestamp? Which classes should I include?

Java Solutions


Solution 1 - Java

No need to get too complicated, try this one liner:

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

Solution 2 - Java

try this one

String fileSuffix = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());

Solution 3 - Java

You can get the current timestamp appended with a file extension in the following way:

String fileName = new Date().getTime() + ".txt";

Solution 4 - Java

You can use DateTime

import org.joda.time.DateTime

Option 1 : with yyyyMMddHHmmss

DateTime.now().toString("yyyyMMddHHmmss")

Will give 20190205214430

Option 2 : yyyy-dd-M--HH-mm-ss

   DateTime.now().toString("yyyy-dd-M--HH-mm-ss")

will give 2019-05-2--21-43-32

Solution 5 - Java

Improving the @Derek Springer post with fill length function:

public static String getFileWithDate(String fileName, String fileSaperator, String dateFormat) {
	String FileNamePrefix = fileName.substring(0, fileName.lastIndexOf(fileSaperator));
	String FileNameSuffix = fileName.substring(fileName.lastIndexOf(fileSaperator)+1, fileName.length());
	//System.out.println("File= Prefix~Suffix:"+FileNamePrefix +"~"+FileNameSuffix);
	
	String newFileName = new SimpleDateFormat("'"+FileNamePrefix+"_'"+dateFormat+"'"+fileSaperator+FileNameSuffix+"'").format(new Date());
	System.out.println("New File:"+newFileName);
	return newFileName;
}

Using the funciton and its Output:

String fileSaperator = ".", format = "yyyyMMMdd_HHmm";
getFileWithDate("Text1.txt", fileSaperator, format);
getFileWithDate("Text1.doc", fileSaperator, format);
getFileWithDate("Text1.txt.json", fileSaperator, format);

Output:

Old File:Text1.txt	 New File:Text1_2020Nov11_1807.txt
Old File:Text1.doc	 New File:Text1_2020Nov11_1807.doc
Old File:Text1.txt.json	 New File:Text1.txt_2020Nov11_1807.json

Solution 6 - Java

Use SimpleDateFormat as aix suggested to format the current time into a string. You should use a format that does not include / characters etc. I would suggest something like yyyyMMddhhmm

Solution 7 - Java

Date, SimpleDateFormat and whatever classes are required on the I/O side of things (there are many possibilities).

Solution 8 - Java

You can use this option

String fileName = new SimpleDateFormat("yyyyMMddHHmmss'.txt'", Locale.getDefault()).format(new Date());

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestiongsbView Question on Stackoverflow
Solution 1 - JavaDerek SpringerView Answer on Stackoverflow
Solution 2 - Javasaigopi.meView Answer on Stackoverflow
Solution 3 - JavaAbdul BasitView Answer on Stackoverflow
Solution 4 - JavaRam GhadiyaramView Answer on Stackoverflow
Solution 5 - JavaYashView Answer on Stackoverflow
Solution 6 - JavaJava DrinkerView Answer on Stackoverflow
Solution 7 - JavaNPEView Answer on Stackoverflow
Solution 8 - JavaAlex View Answer on Stackoverflow