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?
String outFileName = outDir + "\\" + ( new java. sql. Date(today. getTime())) + ".
Timestamp; import java. util. Date; public class TimeStampDemo { public static void main( String[] args ) { //Date object Date date= new Date(); //getTime() returns current time in milliseconds long time = date.
A Timestamp also provides formatting and parsing operations to support the JDBC escape syntax for timestamp values. The precision of a Timestamp object is calculated to be either: 19 , which is the number of characters in yyyy-mm-dd hh:mm:ss. 20 + s , which is the number of characters in the yyyy-mm-dd hh:mm:ss.
No need to get too complicated, try this one liner:
String fileName = new SimpleDateFormat("yyyyMMddHHmm'.txt'").format(new Date());
try this one
String fileSuffix = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With