生成指定时间内的 随机日起

//随机时间
	public static Date getRandomDate(Date start, Date endDate){


		long around= endDate.getTime() - start.getTime();
		Random rand = new Random();
		long random= nextLong(rand,around);

		return  new Date(endDate.getTime()-random);

	}

	public static void main(String[] args) {
		SimpleDateFormat formatter;
		formatter = new SimpleDateFormat ("yyyy.MM.dd G 'at' hh:mm:ss z");
		System.out.println(formatter.format(getRandomDate(new Date("2015/1/1"),new Date())));
	}


	public  int randInt(int min, int max) {

		Random rand = new Random();
		int randomNum = rand.nextInt((max - min) + 1) + min;
		return randomNum;
	}

	public static long nextLong(Random rng, long n) {
		// error checking and 2^x checking removed for simplicity.
		long bits, val;
		do {
			bits = (rng.nextLong() << 1) >>> 1;
			val = bits % n;
		} while (bits-val+(n-1) < 0L);
		return val;
	}

  

posted @ 2015-12-21 22:30  wind90  阅读(168)  评论(0编辑  收藏  举报