This example shares the specific code of Java randomly generated time strings 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 2017/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);//Construction start date Date end = format.parse(endDate);//Construction end date//getTime() means the number of milliseconds represented by this Date object since January 1, 1970 00:00:00 GMT. 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 return start time and end time, recursively call this function 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);//Construction start date System.out.println(resulttime); } }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.