File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Need to find diff between two dates : - 60 days

 
Hemang Thanki
Greenhorn
Posts: 18
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Need to find diff between two dates : - 60 days
I want to use Calendar. I could not figure out what is wrong with below code.
=========================
//Date last address changed
Calendar lastAddChnge = Calendar.getInstance();
lastAddChnge.clear();

lastAddChnge.setTime(lwa.getLastAddrChangeDate());
//Year
int lastAddressChangedDayOfYear = lastAddChnge.DAY_OF_YEAR;

//Current Date
Calendar sysCurrDate = Calendar.getInstance();
sysCurrDate.clear();
sysCurrDate.setTime(lwa.getCurrentSystemDate());

// Current Year
int sysCurrDateDayOfYear = sysCurrDate.DAY_OF_YEAR;

String flag = "false";

if (lastAddChnge.YEAR == sysCurrDate.YEAR)
{
if ((lastAddressChangedDayOfYear - sysCurrDateDayOfYear) <= 60)
flag = "true";
}
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Pie
Posts: 15150
31
Android IntelliJ IDE Java Scala Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What are you trying to do, look at the DAY_OF_YEAR value for both dates and comparing those? That will not work if one date is in 2005 and the other is in 2006, for example.

The DAY_OF_YEAR field returns the day number in the year. For example 20 December 2005 is day number 354, and 8 January 2006 is day number 8. If you subtract them you et 8 - 354 = -346. In other words, DAY_OF_YEAR is not suitable to find out the number of days difference between two dates.

Try this.
 
Hemang Thanki
Greenhorn
Posts: 18
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jesper,

Thanks for your time. I have got answer when I analyzed the code.

Following was wrong:
-------------------
int lastAddressChangedDayOfYear = lastAddChnge.DAY_OF_YEAR;

It should be:
-------------
int lastAddressChangedDayOfYear = lastAddChnge.get(lastAddChnge.DAY_OF_YEAR);

And same is the case with:
int sysCurrDateDayOfYear = sysCurrDate.DAY_OF_YEAR;

All the best for your future ahead.

Thanks a ton.

Hemang.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic