There is a requirement: construct a method and randomly generate any time point between 1990-12-31 00:00 00 and 2013-12-31 00:00:00.
The idea is as follows: in the java API, the Date type and long type are very easy to convert, so we can convert the problem into finding any value between two long type numbers.
At the same time, you need to understand: Math.round(double) Math.random(); new Date(year,month,day); Calendar in the java API.
Math.random() will randomly generate random double types greater than or equal to 0 or less than 1.
Math.round(double) requires passing in a double type to return the long type closest to this double type.
Calendar is a tool class that processes time. Like Date, Calendar's month is also calculated from 0.
The specific code is as follows
public static String randomDateBetweenMinAndMax(){ Calendar calendar = Calendar.getInstance(); //Note that the month needs to be subtracted by 1 calendar.set(1990,11,31); calendar.getTime().getTime(); //According to the requirements, the time, minute and second should be set to 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(); //Get the double value greater than or equal to min less than max double randomDate = Math.random()*(max-min)+min; //Round the double value into an integer and convert it into long type calendar.setTimeInMillis(Math.round(randomDate)); return calendar.getTime().toString(); } If you want to use the Date class to process the time, you need to note that the year starts from 1900, so you need to subtract 1900, and the month starts from 0, so you need to subtract 1. For example, the date object represented by the new Date(2013, 10, 10) is November 10, 3913.
System.out.println(new Date(2013,10,10));
The result is: Mon Nov 10 00:00:00 CST 3913
Change the above problem slightly. Knowing minDate and maxDate requires that each day of this time interval be placed in the list.
public static void getDateBetweenMaxAndMin(){ List<Date> list = new ArrayList<Date>(); Calendar calendar = Calendar.getInstance(); calendar.set(2010,10,10); Date minDate = calendar.getTime(); //Minimum time calendar.set(2013,11,1); Date maxDate = calendar.getTime();//Maximum time//Calculate how many days apart between two time points 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){ //Add 1 calendar.add(Calendar.DAY_OF_MONTH,1); } list.add(calendar.getTime()); } }The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.