Current date + random number + filename (tool class implemented in two ways)

Source: Internet
Author: User

Multi-File upload and download, often need to consider the traditional file multiple times, how to accurately distinguish the name of the file, the following two methods can be achieved
1. The first method of implementation
The path path intercepts the filename from the service side.
String name = path.substring (39);
SimpleDateFormat format = new SimpleDateFormat ("Yyyymmddhhmmss");
String strFileName =format.format (New Date ()) + new Random (). Nextint ();
File File = new file (sdfile + "/video", strfilename+ "-" +name); Create a file object on the Android SDcard

2. The second way of implementation
* Generate Random file name
*/
Public String Generaterandomfilename () {
String randomfilename = "";
Random rand = new Random ();//Generate random number
int random = Rand.nextint ();

Calendar calcurrent = Calendar.getinstance ();
int intday = Calcurrent.get (calendar.date);
int intmonth = Calcurrent.get (calendar.month) + 1;
int intyear = Calcurrent.get (calendar.year);
String now = string.valueof (intyear) + "_" + string.valueof (intmonth) + "_" +
String.valueof (Intday) + "_";
Log.debug ("generated today's filename prefix is:" +now);

Randomfilename = Now + string.valueof (Random > 0? Random: (-1) * random) + ".";

return randomfilename;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.