java - Generate a random number and add 0 before it to ensure fixed number of digits -
so trying generate random number. going append day , month integers before random number. able using following code.
calendar calendar; calendar=calendar.getinstance(); int day= calendar.get(calendar.day_of_month); int month=calendar.get(calendar.month)+1; //it treats jan 0 hence add 1 int num= integer.valueof(string.valueof(month)+string.valueof(day));
now need generate random number add 0s before it. example today 21st september numbers like
921 (num) + 22334 (random num) = 92122334 921 (num) + 2 (random num) = 92100002
basically add 0s start ensuring number of digits remain same. use case of easier way of generating unique order numbers have inbuilt time stamp well. dont expect process more 200 orders day hence taking 5 digit random number seems reasonable enough probability of duplicates small.
two possible solutions.
calendar calendar = gregoriancalendar.getinstance(); int num = 0; num += (calendar.get(calendar.month) + 1) * 10_000_000; num += calendar.get(calendar.day_of_month) * 100_000; num += your_random_number_lower_100000
second
calendar calendar = gregoriancalendar.getinstance(); string randomdigits = string.format("%d%02d%05d", calendar.get(calendar.month) + 1, calendar.get(calendar.day_of_month), your_random_number_lower_100000 );
Comments
Post a Comment