Wednesday, January 20, 2010

Generating unique file names with SimpleDateFormat and Random

Sometimes it is necessary use an unique file name when writing a voice recording with Cisco IP IVR or Cisco Unified Contact Center Express (UCCX). The CCX Editor does support Java, so we can easily do this: let's just create a timestamp with the date and time including milliseconds, then just append a random number between 0 and 9999. Yes, I know there is still some chance this will generate the same string, but this isn't something I cannot live with.

First, create the timeStamp string using this code block:

String DATE_FORMAT="yyyyMMdd_HHmmss-SSS";
java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat(DATE_FORMAT);
return sdf.format(new Date());

Then generate a random number with this code:

java.util.Random nahcis = new java.util.Random();
return nahcis.nextInt(9999).toString();

And in the end just concatenate:

"recording_" + timeStamp + "_" + randomNumber

This will create a string like:


And this is something you can use as the file name.