生成随机日期

/**
		 * 获取随机日期
		 * @param beginDate 起始日期,格式为:yyyy-MM-dd
		 * @param endDate 结束日期,格式为:yyyy-MM-dd
		 * @return
		 */
		private static Date randomDate(String beginDate,String endDate){
			try {
				SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
				Date start = format.parse(beginDate);
				Date end = format.parse(endDate);
				
				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){
         //生成begin和end直接的日期
			long rtn = begin + (long)(Math.random() * (end - begin));

			if(rtn == begin || rtn == end){
				return random(begin,end);
			}
			return rtn;
		}

你可能感兴趣的:(randomdate)