How to Generate Random Number using date and some data in Java?

Java provides different ways to generate random numbers,here to Generate Random Number first we create a GetNumberUsingDateAndRandom method.
this method is take string type data.in this method we first create date object and convert this date in String type.
Now from subString method we print this string type data which start from index 6 and store in dateStr string parameter.
Now we create Random class object which is inside java.util package.It generates a stream of pseudo random numbers.The random.nextInt(int bound) method accepts a parameter bound that should be positive.
After that we add string type data,random value and dateStr date type value.

				
					import java.util.Date;
import java.util.Random;

public class GenerateRandomNumber {
	public static void main(String[] args) {
		String str = "17092023";
		System.out.println("Generate Random Number value is:- " + GetNumberUsingDateAndRandom(str));
	}

	public static String GetNumberUsingDateAndRandom(String type) {
		String dateStr = "";
		Date dt = new Date();
		dateStr = String.valueOf(dt.getTime());
		dateStr = dateStr.substring(dateStr.length() - 6, dateStr.length());
		Random random = new Random();
		int randomVal = random.nextInt(99999999);
		return type + dateStr + randomVal;
	}

}
				
			

Output :-
Generate Random Number value is:- 1709202342267867792001

Leave a Comment

Your email address will not be published. Required fields are marked *