The method previousStandardDate is supposed to do the following:
Given a date and a tick unit, return a date that is unit.getCount() units (month, day, hour, etc) before the date. It should also take into account the tick mark position.
So for instance if you are given March 31, 2004 23:59:59.999 and your unit is a Month with a count of 3, and a tick mark position of END the method should return: December 31, 2003 23:59:59.999
If that is the case, the code is wrong since it will incorrectly return January 31, 2004 23:59:59.999 which is only two months before the passed in date.
The logic isn't taking into account the special cases of a calendar. Instead of using arithmetic, the calendar methods should be used.
I modified the method to be the following and I think it is much easier to understand:
Code: Select all
protected Date previousStandardDate(Date date, DateTickUnit unit) {
Calendar calendar = Calendar.getInstance(this.timeZone);
calendar.setTime(date);
int count = unit.getCount();
switch (unit.getUnit()) {
case (DateTickUnit.MILLISECOND) :
calendar.add(Calendar.MILLISECOND, -count);
return calendar.getTime();
case (DateTickUnit.SECOND) :
calendar.add(Calendar.SECOND, -count);
return calculateDateForPosition(new Second(calendar.getTime()), this.tickMarkPosition);
case (DateTickUnit.MINUTE) :
calendar.add(Calendar.MINUTE, -count);
return calculateDateForPosition(new Minute(calendar.getTime()), this.tickMarkPosition);
case (DateTickUnit.HOUR) :
calendar.add(Calendar.HOUR, -count);
return calculateDateForPosition(new Hour(calendar.getTime()), this.tickMarkPosition);
case (DateTickUnit.DAY) :
calendar.add(Calendar.DATE, -count);
return calculateDateForPosition(new Day(calendar.getTime()), this.tickMarkPosition);
case (DateTickUnit.MONTH) :
calendar.add(Calendar.MONTH, -count);
return calculateDateForPosition(new Month(calendar.getTime()), this.tickMarkPosition);
case(DateTickUnit.YEAR) :
calendar.add(Calendar.YEAR, -count);
return calculateDateForPosition(new Year(calendar.getTime()), this.tickMarkPosition);
default: return null;
}
}
I didn't know how to contribute the code, so I just put it here.
If I'm correct, let me know and I'll respond to the tracker ticket.