java generates a random date for the specified range

  • 2020-12-07 04:02:26
  • OfStack

There is one requirement: construct one method and randomly generate any time point between 1990-12-31 00:00 and 2013-12-31 00:00:00.
Here's the idea: Date and long types convert well in javaAPI, so we can turn the problem into finding any value between the two long types.
Also need to know Math API: Math.round(double) Math.random(); new Date(year,month,day); Calendar.

Math.random() randomly generates random double types greater than or equal to 0 and less than 1.

Math. round(double) needs to pass in one double type to return the long type closest to this double type.

Calendar is a utility class for processing time, and the month of Calendar, like Date1, is also calculated from 0

The code is as follows


public static String randomDateBetweenMinAndMax(){ 
    Calendar calendar = Calendar.getInstance(); 
    // Notice we're subtracting the months 1 
    calendar.set(1990,11,31); 
    calendar.getTime().getTime(); 
    // Depending on your requirements, set the time to minutes and seconds 0 
    calendar.set(Calendar.HOUR_OF_DAY, 0); 
    calendar.set(Calendar.MINUTE, 0); 
    calendar.set(Calendar.SECOND,0); 
    long min = calendar.getTime().getTime();; 
    calendar.set(2013,11,31); 
    calendar.set(Calendar.HOUR_OF_DAY,0); 
    calendar.set(Calendar.MINUTE,0); 
    calendar.set(Calendar.SECOND,0); 
    calendar.getTime().getTime(); 
    long max = calendar.getTime().getTime(); 
    // You get greater than or equal to min Less than max the double value  
    double randomDate = Math.random()*(max-min)+min; 
    // will double The value is rounded to an integer and converted to long type  
    calendar.setTimeInMillis(Math.round(randomDate)); 
    return calendar.getTime().toString(); 
  } 

If you want to use the Date class processing time, it's important to note that the year is calculated from 1900 so you subtract 1900, and the month is calculated from 0 so you subtract 1, for example, new Date(2013,10,10) and the date object represents the date of November 10, 3913.


System.out.println(new Date(2013,10,10)); 

The result: Mon Nov 10 00:00:00 CST 3913

To change the above question slightly, given minDate and maxDate, it is required to put the time interval into list every 1 day.


public static void getDateBetweenMaxAndMin(){ 
    List<Date> list = new ArrayList<Date>(); 
    Calendar calendar = Calendar.getInstance(); 
    calendar.set(2010,10,10); 
    Date minDate = calendar.getTime(); // The minimum time  
    calendar.set(2013,11,1); 
    Date maxDate = calendar.getTime();// Maximum time  
    // Calculate how many days apart the two time points are  
    int totalDays = Ints.checkedCast((maxDate.getTime() - minDate.getTime()) / (1000 * 60 * 60 * 24)); 
    calendar.setTime(minDate); 
    calendar.set(Calendar.HOUR_OF_DAY,0); 
    calendar.set(Calendar.MINUTE,0); 
    calendar.set(Calendar.SECOND,0); 
    for(int i = 0;i<=totalDays;i++){ 
      if(i!=0){ 
        // Number of days to add 1 
        calendar.add(Calendar.DAY_OF_MONTH,1); 
      } 
      list.add(calendar.getTime()); 
    } 
  } 

Related articles: