<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Technology Bits and Bytes &#187; Nivasini</title>
	<atom:link href="http://blogs.circlesource.com/author/nivasini/feed/" rel="self" type="application/rss+xml" />
	<link>http://blogs.circlesource.com</link>
	<description>CircleSource Technical Talent ShowCase</description>
	<lastBuildDate>Thu, 10 Dec 2009 20:01:18 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Generating random Date/Time between 2 different Dates/TimeStamps in Java</title>
		<link>http://blogs.circlesource.com/2008/07/17/generating-random-datetime-between-2-different-datestimestamps-in-java/</link>
		<comments>http://blogs.circlesource.com/2008/07/17/generating-random-datetime-between-2-different-datestimestamps-in-java/#comments</comments>
		<pubDate>Thu, 17 Jul 2008 13:34:10 +0000</pubDate>
		<dc:creator>Nivasini</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Random dates]]></category>

		<guid isPermaLink="false">http://blogs.circlesource.com/?p=8</guid>
		<description><![CDATA[It is common to find a random number between 2 given numbers&#8230; In our java application that we are working on, We had a need to find a random time between two given timestamp.. We felt it little challenging in the beginning&#8230; But later figured out a simple solution for the same&#8230; The steps for [...]]]></description>
			<content:encoded><![CDATA[<p>It is common to find a random number between 2 given numbers&#8230; In our java application that we are working on, We had a need to find a random time between two given timestamp.. We felt it little challenging in the beginning&#8230; But later figured out a simple solution for the same&#8230; The steps for generating a random time between 2 timestamps really turned out to be very simple &amp; this article is about how we achieved that&#8230;</p>
<p>If min and max are the 2 variables that are holding minimum and maximum values, &amp; random is the random number that is to be generated, in java the random number is calculated in this way&#8230;<br />
<strong><code><br />
random = (Math.random() * (max - min) ) + min<br />
</code></strong></p>
<p>Here <strong>Math.random()</strong> is a function in java that generates any random value between 0 &amp; 1&#8230; We multiply that with the difference between max &amp; min values and finally add the obtained result with the min value to get a random number between 2 given numbers.. This idea was set as the basement for our requirement too&#8230; Wondering how?? This is how it goes&#8230;</p>
<p>We use <strong>Calendar</strong> object which defaults to current date and time. We then set Hour, minute &amp; second in the same object(If required we can set the year, month &amp; date too).. Now we have the Calendar object with the date what we set or current date and time. Calendar class has a method named <strong>getTimeInMillis</strong> which will convert this timeStamp to a long value. This long value serves as the first min variable as in the example where we generate a random number between 2 different numbers.</p>
<p>Now in the same way we get the long value of the second timestamp too which is the max variable value. Using a similar calculation as above we get a random long number between 2 different long numbers which are the long time. Finally we construct the date object with the long value that we obtained.. This date object&#8217;s value is the random time stamp between 2 different Calendar objects and hence the we can generate random date/time between two different Calendar objects!!!!   <img src='http://blogs.circlesource.com/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
<p>Here goes the code for that..<br />
<strong><code><br />
//code to generate random timestamp between morning 6 to evening 8 pm<br />
public static void main(String args[]){<br />
Calendar cdr = Calendar.getInstance();<br />
cdr.set(Calendar.HOUR_OF_DAY, 6);<br />
cdr.set(Calendar.MINUTE, 0);<br />
cdr.set(Calendar.SECOND, 0);<br />
long val1=cdr.getTimeInMillis();<br />
</code></p>
<p><code><br />
cdr.set(Calendar.HOUR_OF_DAY, 20);<br />
cdr.set(Calendar.MINUTE, 0);<br />
cdr.set(Calendar.SECOND, 0);<br />
long val2=cdr.getTimeInMillis();<br />
</code></p>
<p><code><br />
Random r=new Random();<br />
long randomTS=(long)(r.nextDouble()*(val2-val1))+val1;<br />
Date d=new Date(randomTS);<br />
System.out.println(d.toString());<br />
}<br />
</code></strong><br />
<strong>PS: This will work even for generating random date between two different dates which can be a month/year gap.</strong></p>
]]></content:encoded>
			<wfw:commentRss>http://blogs.circlesource.com/2008/07/17/generating-random-datetime-between-2-different-datestimestamps-in-java/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>
