wood burning stoves 2.0*
The moose likes Java in General and the fly likes Need to find diff between two dates : - 60 days Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Need to find diff between two dates : - 60 days" Watch "Need to find diff between two dates : - 60 days" New topic
Author

Need to find diff between two dates : - 60 days

Hemang Thanki
Greenhorn

Joined: Jan 24, 2005
Posts: 18
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

Joined: Aug 16, 2005
Posts: 14074
    
  16

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.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Hemang Thanki
Greenhorn

Joined: Jan 24, 2005
Posts: 18
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
 
subject: Need to find diff between two dates : - 60 days
 
Similar Threads
Calculate Business Days between two dates using java
Number of weeks between two dates
Difference Between two dates
Differnce between time.
how to get date month year time