Android get date and insert to filename

江枫思渺然 提交于 2019-12-04 06:20:28

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";

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) {}

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();
Raghav Chopra

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

How to use SimpleDateFormat to show the current Date?

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!