首页 > 其他 > 详细

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

时间:2015-12-21 23:38:50      阅读:311      评论:0      收藏:0      [点我收藏+]
//随机时间
	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;
	}

  

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

原文:http://www.cnblogs.com/wind90/p/5065086.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!