Java generates a random date in the specified range, and java generates a range

Source: Internet
Author: User

Java generates a random date in the specified range, and java generates a range

There is a need to construct a method to randomly generate any time point between 00:00:00 and.
The idea is as follows: in Java API, the Date type and long type are well converted, so we can convert the problem to evaluate any value between the two long types.
You also need to know Math. round (double) Math. random (); new Date (year, month, day); Calendar ar in java APIs.

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

Math. round (double) needs to input a double type and return the long type closest to this double type.

Calendar is a tool class for processing time. Like Date, Calendar month is calculated from 0.

The Code is as follows:

Public static String randomDateBetweenMinAndMax () {Calendar calendar = Calendar. getInstance (); // note that the month minus 1 calendar ar. set (1990,11, 31); calendar. getTime (). getTime (); // set the time, minute, and second to 0 calendar as required. set (Calendar. HOUR_OF_DAY, 0); calendar. set (Calendar. MINUTE, 0); calendar. set (Calendar. SECOND, 0); long min = calendar. getTime (). getTime (); calendar. set (maid, 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 (); // obtain the double value greater than or equal to min and less than max. double randomDate = Math. random () * (max-min) + min; // round the double value to an integer and convert it to the 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 is calculated from 1900, so you need to subtract 1900, and the month is calculated from 0. Therefore, you need to subtract 1, such as new Date (, 10) the date object is represented on January 1, November 10, 3913.

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

Result: Mon Nov 10 00:00:00 CST 3913

Change the preceding problem slightly. If minDate and maxDate are known, you need to put every day of the time range into 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 (); // the minimum time of calendar. set (maid, 1); Date maxDate = calendar. getTime (); // maximum time // calculate the number of days between two time points int totalDays = Ints. checkedCast (maxDate. getTime ()-minDate. getTime ()/(1000*60*60*24); calendar. setTime (minDa Te); 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 for your learning and support for helping customers.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.