Java random generation time string method, java generation string
The examples in this article share with you the specific code of the java random generation time string for your reference. The specific content is as follows:
Package com. wechat. utils; import java. text. simpleDateFormat; import java. util. date;/*** Created by hexun on instances /2/4. */public class RandTimeUtils {/*** generate random time * @ param beginDate * @ param endDate * @ return */private static Date randomDate (String beginDate, String endDate) {try {SimpleDateFormat format = new SimpleDateFormat ("yyyy-MM-dd"); Date start = format. parse (beginDate); // construct the start Date en D = format. parse (endDate); // construct the end Date // getTime () indicates the number of milliseconds that the Date object represents since 00:00:00 GMT, January 1, January 1, 1970. If (start. getTime ()> = end. getTime () {return null;} long date = random (start. getTime (), end. getTime (); return new Date (date);} catch (Exception e) {e. printStackTrace ();} return null;} private static long random (long begin, long end) {long rtn = begin + (long) (Math. random () * (end-begin); // If the returned start time and end time are, call this function recursively to find the random value if (rtn = begin | rtn = end) {return random (begin, end) ;}return rtn ;} public static void main (String [] args) {Date randomDate = randomDate ("2010-09-20", "2017-02-04"); SimpleDateFormat format = new SimpleDateFormat ("yyyy-MM-dd HH: mm: ss "); String resulttime = format. format (randomDate); // construct the start date System. out. println (resulttime );}}
The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.