Reputation: 2126
I am using Calendar Class in my android app to calulate day of year of a date and then do some comparison with current day of year. Here is the code I use :
Date now=new Date();
Calendar ca1 = Calendar.getInstance();
ca1.set(now.getYear(), now.getMonth(), now.getDate());
int nowC=ca1.get(Calendar.DAY_OF_YEAR);
//Date arg0=say,get from user
Calendar ca2 = Calendar.getInstance();
ca2.set(arg0.GetBirthDay().getYear(),arg0.GetBirthDay().getMonth(),arg0.GetBirthDay().getDate());
int d1C=ca2.get(Calendar.DAY_OF_YEAR);
I debug my application and I see the following value for current day : Fri Mar 02 14:18:33 Asia/Tehran 2012
and for arg0: Fri Mar 02 00:00:00 Asia/Tehran 1979
And 'nowC' got 62, and 'd1C' got 61.
I expect them to be equal cause both of them has same month and day, also If I use DateTime class of joda package, as below, I get the same results:
int ndy=dtnow.getDayOfYear();
int d1dy=dt1.getDayOfYear();
Why it is happening ?
Upvotes: 0
Views: 196
Reputation: 318
2012 was a leap year, 1979 was not. There is an extra day before March 2nd this year - so both APIs are giving you the right answer!
I suspect you want to compare both the month and day-of-month, to get the semantics you are expecting.
Upvotes: 2