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

//随机时间
	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;
	}

  

你可能感兴趣的:(生成指定时间内的 随机日起)