Thursday, 16 June 2016

Generating unique random numbers and alphanumeric values in Java

Below code generates unique random numbers and alphanumeric values in Java.

Calendar date = Calendar.getInstance();
        String day = String.valueOf(date.get(Calendar.DAY_OF_MONTH));
        String month = String.valueOf(date.get(Calendar.MONTH)+1);
        String year = String.valueOf(date.get(Calendar.YEAR));

        if (day.length()==1){day = "0"+day;}
        if (month.length()==1){month = "0"+month;}

        String time =  String.valueOf(date.get(Calendar.HOUR_OF_DAY)) 
+ String.valueOf(date.get(Calendar.MINUTE)) +
 String.valueOf(date.get(Calendar.SECOND));
        System.out.println("Unique timeStamp is -> " + time +
 year + month + day);

        //Generates random hash string containing numbers and digits
        UUID id1 = UUID.randomUUID();
        System.out.println("Unique id is -> "+ id1);

        java.util.Date date1= new java.util.Date();
        System.out.println("Unique timestamp is -> " +
 new Timestamp(date1.getTime()));


What do you think on above selenium topic. Please provide your inputs and comments. You can write to me at reply2sagar@gmail.com

No comments:

Post a Comment

Buy Best Selenium Books

Contributors