Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android get date and insert to filename

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)

like image 628
Morten Hagh Avatar asked Nov 18 '11 11:11

Morten Hagh


1 Answers

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) {}
like image 96
Caner Avatar answered Sep 24 '22 15:09

Caner