Two Methods of Getting Random Date Objects (java.sql.Date) with Specified Range in Java

Keywords: Java SQL

Links to the original text: https://blog.csdn.net/WuLex

Or because filling in self-built data tables requires a range of random dates, with the goal of being elegant, concise and efficient.
There are two ideas:

  1. Generate a required 13-bit random number as milliseconds and convert milliseconds into Date classes in java.sql packages
  2. A random year is generated, and then a random day is generated according to the range of days determined by the year and month.

First code, then efficiency comparison.
The designated scope is 2016-2018 (i.e. 1 January 2016-31 December 2018).
Method of generating thirteen random numbers:

	public static Date randomHireday() {
		int startYear=2016;									//Starting year of specified random date
		int endYear=2018;									//Starting year of specified random date (including)
		long start = Timestamp.valueOf(startYear+1+"-1-1 0:0:0").getTime();
		long end = Timestamp.valueOf(endYear+"-1-1 0:0:0").getTime();
		long ms=(long) ((end-start)*Math.random()+start);	//The qualified number of 13-bit milliseconds is obtained.
		Date hireday=new Date(ms);
		return hireday;
	}

Ten cycles:

Random years and days are generated respectively:

	public static Date randomHireday2() {
		int startYear=2016;
		int endYear=2018;
		int year = (int)(Math.random()*(endYear-startYear+1))+startYear;	//Random year
		int month= (int)(Math.random()*12)+1;								//Random Month
		Calendar c = Calendar.getInstance();				//Create Calendar objects
		c.set(year, month, 0);								//Setting Date
		int dayOfMonth = c.get(Calendar.DAY_OF_MONTH);		//How many days to get the corresponding year and month
		int day=(int)(Math.random()*dayOfMonth+1)	;		//Generating random days
		Date hireday=Date.valueOf(year+"-"+month+"-"+day);	//Generating Date Object by valueOf Method
		return hireday;
	}

Ten cycles:

Now start testing efficiency, 10 short tests and 100,000 long tests, respectively.

	public static void main(String[] args) {
		//Random thirteen digits
		long start;
		long end;
		start = System.currentTimeMillis();
		for(int i=0;i<10;i++) {
			randomHireday();
		}
		end = System.currentTimeMillis();
		System.out.println("Time spent in 10 cycles of random 13 digits:"+(end-start));
		
		start = System.currentTimeMillis();
		for(int i=0;i<100000;i++) {
			randomHireday();
		}
		end = System.currentTimeMillis();
		System.out.println("Time spent in 100,000 random 13-digit cycles:"+(end-start));
		
		//Random generation year, month and day
		start = System.currentTimeMillis();
		for(int i=0;i<10;i++) {
			randomHireday2();
		}
		end = System.currentTimeMillis();
		System.out.println("Time spent in 10 cycles of each random generation year, month and day:"+(end-start));
		
		start = System.currentTimeMillis();
		for(int i=0;i<100000;i++) {
			randomHireday2();
		}
		end = System.currentTimeMillis();
		System.out.println("Time spent in 100,000 cycles of random generation year, month and day:"+(end-start));
	}


Obviously, the more concise and elegant method of thirteen random numbers is more efficient.
Thus, we can get an elegant, concise and efficient random date generation method.

Posted by bjblackmore on Wed, 09 Oct 2019 18:45:31 -0700