Android get date and paste in filename

I have a pretty nasty problem. I want to get the current date / time and paste it into the file name, but I cannot make it work in my life.

I want to get the time as 2011-11-18 12:13:57 and then paste it in my file name

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

I tried SimpleDateFormat , etc., but that just won't work!

(I write in Eclipse Indigo)

+4
source share
3 answers

You can use this:

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

You should also get an error somewhere that would help a lot to find the problem. Make sure you don't have any empty catch blocks:

 catch (Exception e) {} 
+19
source

Use this to get the right time:

 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 as follows:

 FileOutputStream out = context.openFileOutput(timeStamp , context.MODE_PRIVATE); out.write(string.getBytes()); out.close(); 
0
source

Hope this helps you get the current date and time.

How to use SimpleDateFormat to display current date?

-1
source

Source: https://habr.com/ru/post/1381888/


All Articles