Current timestamp as filename in Java
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?
Solution 1:
No need to get too complicated, try this one liner:
String fileName = new SimpleDateFormat("yyyyMMddHHmm'.txt'").format(new Date());
Solution 2:
try this one
String fileSuffix = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
Solution 3:
You can get the current timestamp appended with a file extension in the following way:
String fileName = new Date().getTime() + ".txt";
Solution 4:
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