Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

append a file name with timestamp

 
Gopu Akraju
Ranch Hand
Posts: 242
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to append a file name with time stamp so that every file will have a unique file name. I tried as below:



This works fine as the file name gets appended with 2008-04-07.txt Now I want to add timestamp with it to make it unique. I tried as below:
It is throwing error.
When I try to print the above piece of code, it prints as below:
But it is not allowing me to write a file as with that name filename_2008-04-07 10:48:05.083.txt (The parameter is incorrect) Is it due to the gap between date and time stamp. How do I overcome this? Thanks.
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you using Windows ? In Windows Explorer, try to create a file whose name is the filename you have posted above. What does it say ?
 
Gopu Akraju
Ranch Hand
Posts: 242
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your response. I could finish it as below:



Now I could append the filename with date_time so that it each file name is unique.
 
Rob Spoor
Sheriff
Pie
Posts: 20527
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Gopu Akraju:
Thanks for your response. I could finish it as below:



Now I could append the filename with date_time so that it each file name is unique.

You can use one single method as well:

It will be just a bit more efficient - only one SimpleDateFormat and Date object for each file, as well as no String concatenation.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic