Generate 10 Digit unique number
Generate 10 Digit unique number on Date
Use the following code for creating 10 digit unique number on the basis of dates. It is unique in seconds level not design for milliseconds.
this program will create unique numbers upto 52 years, reset it with changing 'adder' value.
Author : Sunil Wagh (SCJP, SCWCD, OCP , IBM Maximo 7 Certified)
--------------------------------------------------------------------------
import java.text.DecimalFormat;
import java.util.Calendar;
public class Demo2 {
// truncate four digit to two digit 2010=>10
public static int retval(int y){
String yrs=""+y;
yrs=yrs.substring(1);
y = Integer.parseInt(yrs) ;
return y;
}
// Create 10 Digit Unique number
public void test(){
int adder = 55;//Number Start from year 2010, change 64 to start from 2001
Calendar cal = Calendar.getInstance();
int year = cal.get(Calendar.YEAR);
int doy = cal.get(Calendar.DAY_OF_YEAR);
int hr = cal.get(Calendar.HOUR_OF_DAY);
int mi = cal.get(Calendar.MINUTE);
int ss = cal.get(Calendar.SECOND);
int yr = adder+retval(year);
char c=(char)yr;
DecimalFormat df = new DecimalFormat("000");
System.out.println("Unique Num: " + c + df.format(doy) + hr + mi + ss);
}
public static void main(String a[]){
Demo2 d = new Demo2();
d.test();
}
}
-------------------------------------------------------------------------
Output : Unique Num: A23117587
------------------------------------------------------------------------
Re: Generate 10 Digit unique number
Thanks for the code, however I was needing a script, where basically the user has to enter his name, choose file, file comments if required and upload and I want to generate a 10 digit unique code everytime, which makes it simple for the user to download the file. I am posting the below code, can you help me out:
PHP Code:
<?
//set the random id length
$random_id_length = 10;
//generate a random id encrypt it and store it in $rnd_id
$rnd_id = crypt(uniqid(rand(),1));
//to remove any slashes that might have come
$rnd_id = strip_tags(stripslashes($rnd_id));
//Removing any . or / and reversing the string
$rnd_id = str_replace(".","",$rnd_id);
$rnd_id = strrev(str_replace("/","",$rnd_id));
//finally I take the first 10 characters from the $rnd_id
$rnd_id = substr($rnd_id,0,$random_id_length);
echo "Random Id: $rnd_id";
?>