<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:blogger='http://schemas.google.com/blogger/2008' xmlns:georss='http://www.georss.org/georss' xmlns:gd="http://schemas.google.com/g/2005" xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-3223681509461752208</id><updated>2024-09-29T00:13:01.234-04:00</updated><category term="java"/><title type='text'>memymo</title><subtitle type='html'>Just my thoughts</subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://memymo-daily.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3223681509461752208/posts/default?redirect=false'/><link rel='alternate' type='text/html' href='http://memymo-daily.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>memymo</name><uri>http://www.blogger.com/profile/17607526342805648053</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>1</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>25</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-3223681509461752208.post-7997820546371314752</id><published>2010-08-03T11:46:00.002-04:00</published><updated>2013-03-06T13:57:59.920-05:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="java"/><title type='text'>java date compare</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;

&lt;br /&gt;
&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
if you ever tried to compare java dates, you will get unexpected results if you do not consider time component of java date, for example if you try to&lt;b&gt; compare&lt;/b&gt; 2010-08-03 with 2010-08-01,&lt;br /&gt;
then you can see that date1 is greater and the difference is 2 day, but if you write java code as below.&lt;br /&gt;
&lt;pre class=&quot;brush: java&quot;&gt;
import java.util.Calendar;
 
public class TestDateCompare {
 
    private final long MILLSECS_PER_DAY = 24 * 60 * 60 * 1000;
 
    public TestDateCompare() throws InterruptedException{
 
        Calendar today = Calendar.getInstance();        
 
     // Add 2 second delay so we can have difference between 2 dates
 
        Thread.sleep(2000);        
 
        Calendar yesterday = Calendar.getInstance();
 
        //Substract 2 days from today
 
        yesterday.add(Calendar.DATE, -2);
 
        long numOfDaysDifference = ( today.getTime().getTime() - yesterday.getTime().getTime() )/ MILLSECS_PER_DAY;
 
         //This should print output as 2, but it prints output as 1, 
 
        System.out.println(&quot;Num of days difference &quot; + numOfDaysDifference);
 
        System.out.println(&quot;Yesterday &quot; + yesterday.getTime());
 
        System.out.println(&quot;Today &quot; + today.getTime());
 
        }
 
    public static void main(String[] args) throws InterruptedException {
 
        new TestDateCompare();
 
    }
 
}
&lt;/pre&gt;
&lt;div&gt;
The out put of this class is&lt;/div&gt;
&lt;div style=&quot;border: 1px dotted #ccc; color: green; overflow-x: auto; overflow: auto; padding: 5px 5px 5px 7px;&quot;&gt;
&lt;div&gt;
Num of days difference 1&lt;/div&gt;
&lt;div&gt;
Yesterday Sun Aug 01 11:31:43 EDT 2010&lt;/div&gt;
&lt;div&gt;
Today Tue Aug 03 11:31:41 EDT 2010&lt;/div&gt;
&lt;/div&gt;
&lt;div&gt;
As you can see there is 2 seconds difference, which throws the calculation off, so to make this work add following lines of code before doing date calculation&lt;/div&gt;
&lt;div style=&quot;border: 1px dotted #ccc; color: blue; overflow-x: auto; overflow: auto; padding: 5px 5px 5px 7px;&quot;&gt;
&lt;div&gt;
&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;today.set(Calendar.HOUR, 0);&lt;/div&gt;
&lt;div&gt;
&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;today.set(Calendar.MINUTE, 0);&lt;/div&gt;
&lt;div&gt;
&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;today.set(Calendar.SECOND, 0);&lt;/div&gt;
&lt;div&gt;
&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;today.set(Calendar.MILLISECOND, 0);&lt;/div&gt;
&lt;div&gt;
&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;/div&gt;
&lt;div&gt;
&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;yesterday.set(Calendar.HOUR, 0);&lt;/div&gt;
&lt;div&gt;
&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;yesterday.set(Calendar.MINUTE, 0);&lt;/div&gt;
&lt;div&gt;
&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;yesterday.set(Calendar.SECOND, 0);&lt;/div&gt;
&lt;div&gt;
&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;yesterday.set(Calendar.MILLISECOND, 0);&lt;/div&gt;
&lt;/div&gt;
Do not forget millisecond also, as it may throw off the calculation&lt;br /&gt;
After this change the output is as below&lt;br /&gt;
&lt;br /&gt;
&lt;div style=&quot;border: 1px dotted #ccc; color: green; overflow-x: auto; overflow: auto; padding: 5px 5px 5px 7px;&quot;&gt;
Num of days difference 2&lt;br /&gt;
Yesterday Sun Aug 01 00:00:00 EDT 2010&lt;br /&gt;
Today Tue Aug 03 00:00:00 EDT 2010&lt;/div&gt;
&lt;br /&gt;
There are many other alternatives, like using &lt;a href=&quot;http://mindprod.com/jgloss/bigdate.html&quot; target=&quot;_blank&quot; title=&quot;BigDate&quot;&gt;BigDate&lt;/a&gt;&amp;nbsp;or &lt;a href=&quot;http://joda-time.sourceforge.net/&quot; target=&quot;_blank&quot; title=&quot;Joda-Time&quot;&gt;joda-time&lt;/a&gt;,&lt;br /&gt;
If you have anymore solutions, please feel free to add to this list&lt;/div&gt;
&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://memymo-daily.blogspot.com/feeds/7997820546371314752/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/3223681509461752208/7997820546371314752' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3223681509461752208/posts/default/7997820546371314752'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3223681509461752208/posts/default/7997820546371314752'/><link rel='alternate' type='text/html' href='http://memymo-daily.blogspot.com/2010/08/java-date-compare.html' title='java date compare'/><author><name>memymo</name><uri>http://www.blogger.com/profile/17607526342805648053</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry></feed>