Richard HALLIER wrote:
> My customer asks me for calculating the number of months between 2 dates
> with the following pattern (actually an example):
> 
> start date = 17 january 2006
> end date = 28 april 2006
> 
> The expected result is :
>   (31-17+1)/31   (31 days in january)
> + 2              (february and march)
> + (28/30)        (30 days in april)
> = 3.42

That feels like an unusual calculation design, although I'd welcome 
anyone else's comments on the merits of the algorithm.

Joda-Time, does not really have any support for this at present. So, you 
will need to treat the three parts (first month, in between months, last 
month) separately.

The new Joda-Time 1.4 will contain classes named Days, Hours, Minutes 
and Seconds. These may be useful here:

LocalDate start = new LocalDate(2006, 1, 17);
LocalDate endOfMonth = start.dayOfMonth().withMaximumValue();

Days days = Days.daysBetween(firstOfMonth, start);
days = days.plus(1);  // needed to to count days inclusively
Seconds secs = days.toStandardSeconds();

Days daysInMonth = Days.days(start.dayOfMonth().getMaximumValue());
Seconds secondsInMonth = daysInMonth.toStandardSeconds();

double fractionInMonth = secs.getSeconds() / secondsInMonth.getSeconds();

Although this algorithm could be repeated for each month, it will be 
more efficient to only use it on those months which are fractional.


I guess the question for Joda-Time is whether support for decimal 
calculations like this is needed. For example, the Days class has a 
dividedBy(int) method which returns another Days and uses int division. 
Maybe we should have a dividedBy(Days) that returns a double or a 
BigDecimal?

Stephen

-------------------------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
_______________________________________________
Joda-interest mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/joda-interest

Reply via email to