9

I am having a quite annoying problem. I want to get the current date/time and insert it into a filename but I can't for my life get it to work.

I want to get the time as 2011-11-18 12:13:57 and then insert it into my filename

filename-2011-11-18-12:13:57.tar.gz

I have tried SimpleDateFormat etc. but it just won't work!

(I am writing in Eclipse Indigo)

Caner
  • 57,267
  • 35
  • 174
  • 180
Morten Hagh
  • 2,055
  • 8
  • 34
  • 66
  • 4
    I think,it would be an invalid file name? are you getting any error or so? – Hiral Vadodaria Nov 18 '11 at 11:59
  • I get no errors, it just don't create any file when I press the button... I try it like filename-"+date+".tar.gz If i remove "+date+" it creates the file without any problems. – Morten Hagh Nov 18 '11 at 12:05
  • then you must be using invalid filename only.try making file name like filename_2011_11_18_12_13_57.tar.gz. It won't create problem then. – Hiral Vadodaria Nov 18 '11 at 12:07
  • replace the symbol '-' and ':' with '_' in date and then try it.it sholud work – user370305 Nov 18 '11 at 12:08
  • I think @Hiral is correct about the invalid file name. Try something like `filename_2011-11-18_12-13-57.tar.gz`. In other words, separate `filename` from the `date` with an underscore and separate the `date` from the `time` with an underscore and replace the `:` in the `time` with a `-`. This would be much easier to `split` if necessary and would use valid characters for a file name. – Squonk Nov 18 '11 at 12:08
  • Like others, I'm suspicious of those colon characters, but it might be helpful if you posted an example of the code you have tried, just in case it is something else. – Steve Bosman Nov 18 '11 at 12:14

3 Answers3

31

You can use this:

import java.text.SimpleDateFormat;
// ...

SimpleDateFormat formatter = new SimpleDateFormat("yyyy_MM_dd_HH_mm_ss", Locale.US);
Date now = new Date();
String fileName = formatter.format(now) + ".tar.gz";

Also you must be getting an error somewhere, that would help a lot to find the problem. Make sure you don't have empty catch blocks :

catch (Exception e) {}
Caner
  • 57,267
  • 35
  • 174
  • 180
  • If I use this one Eclipse tells me that date() is undefined and I need arguments to match it... I can get it to print the date but the date is 01_01_1970 – Morten Hagh Nov 18 '11 at 12:35
  • Then add `import java.util.Date;` – Caner Nov 18 '11 at 12:37
  • I cannot believe that this was the simple solution! I have been messing with this for 2 days! I cannot thank you enough! :) Eclipse have automatically imported everything else, but not this one! Thank you! – Morten Hagh Nov 18 '11 at 12:44
  • 1
    You can get rid of the warning in Eclipse for SimpleDateFormat by specifying a known safe Locale. For example SimpleDateFormat formatter = new SimpleDateFormat("yyyy_MM_dd_HH_mm_ss", Locale.US); – Pooks Apr 17 '15 at 02:22
0

Use this to get the date time you want :

SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = new Date();
String timeStamp = dateFormat.format(date.toLocaleString());

and you can create your file like this :

FileOutputStream out = context.openFileOutput(timeStamp , context.MODE_PRIVATE);
out.write(string.getBytes());
out.close();
Android-Droid
  • 14,365
  • 41
  • 114
  • 185
  • -1 `dateformat` is created but not used anywhere? Also why woudl you copy a string to another string for no purpose?: `String lastSyncTime = timeStamp;` – Caner Nov 18 '11 at 12:22
  • I just copy and paste the code which I was using in one application, that's why – Android-Droid Nov 18 '11 at 12:26
  • ok, but still `dateFormat` is not used anywhere so could be removed.And why did you give me -1? – Caner Nov 18 '11 at 12:31
  • ok, but now the date he will get will still be like `2011-11-18 12:13:57` and it doesn't work for him.(see comments under question) – Caner Nov 18 '11 at 12:38
-1

I hope this could help u out to get the current date and time

How to use SimpleDateFormat to show the current Date?

Community
  • 1
  • 1
Raghav Chopra
  • 527
  • 1
  • 10
  • 26
  • That's a terrible way to get the current date. There are already Java functions for this purpose. Plus the problem the poster is facing is not getting the date, but creating the file. – Caner Nov 18 '11 at 12:20
  • i have clearly mentioned above that this is to get the cureent not to save in the file@LAS_VEGAS – Raghav Chopra Nov 18 '11 at 12:33
  • sorry my bad, but I think this is still a very bad way to get current date. Because you can get the current date as string with couple of lines of code. Your logic might have some bug somewhere where it would be difficult to spot. – Caner Nov 18 '11 at 12:52