Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Current timestamp as filename in Java

Tags:

java

timestamp

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?

like image 782
gsb Avatar asked Sep 20 '11 16:09

gsb


People also ask

How do I add a timestamp to a Java file?

String outFileName = outDir + "\\" + ( new java. sql. Date(today. getTime())) + ".

What is current timestamp in Java?

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.

What is timestamp format in Java?

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.


2 Answers

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

String fileName = new SimpleDateFormat("yyyyMMddHHmm'.txt'").format(new Date()); 
like image 171
Derek Springer Avatar answered Oct 01 '22 11:10

Derek Springer


try this one

String fileSuffix = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()); 
like image 27
saigopi.me Avatar answered Oct 01 '22 12:10

saigopi.me