Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create file name using date and time

I hope you could help me, I'm trying to call in the date from another class and looks like "2011-03-09 06-57-40", I want to use this to create the file below but everytime I do when the output runs it creates a new file as it re-runs calling the dat(). I know what's going wrong I'm just not sure how to fix it, I want to permently writw to the same file. I hope this makes sense? :/

Thank you for any help in advance :)

    date d = new date();     String  cdate = d.date();       String f = h;      try{         PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(cdate + ".tsv", true)));         out.print(f);         out.print("\t");         out.close();     }catch (IOException e){     } 
like image 748
Tom Avatar asked Mar 09 '11 07:03

Tom


People also ask

How do I name a file with date and time?

I'd use YYYY-MM-DD HHmmss for filenames, unless there is a particular need for timezones or a possible need to parse them into ISO dates; in those cases an ISO date would probably be preferrable.

How do you create a filename with a date and time in python?

First, import the module and then get the current time with datetime. now() object. Now convert it into a string and then create a file with the file object like a regular file is created using file handling concepts in python.


1 Answers

To create a file named the current date/time:

Date date = new Date() ; SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss") ; File file = new File(dateFormat.format(date) + ".tsv") ; BufferedWriter out = new BufferedWriter(new FileWriter(file)); out.write("Writing to file"); out.close(); 
like image 130
Rune Aamodt Avatar answered Oct 07 '22 16:10

Rune Aamodt