I suppose I should note that this is the second possible example of a requirement for multiply/divide on a duration.

Stephen

Stephen Colebourne wrote:



Carl Henrik Janson wrote:

Thanks for providing joda time!

Thanks for your interest

            DateTime end = new DateTime(2006, 3, 16, 9, 21, 0, 0);
            DateTime start = new DateTime(1960, 3, 1, 9, 45, 0, 0);
         Period period = new Period(start, end, PeriodType.yyy());
         double daysOldWithFraction = period.xxx();

I need to produce a number like the format used in excel, inter alias 16815,98 in this example. How do I do that ? Am I on track ? What xxx and yyy produces what I need ?


Unfortunately, the Joda-Time model is not setup to suport fractional days, only fractional seconds.

There is no simple workaround, as we can't assume that a day is necessarily 24 hours long. Thus, to support your request, the whole model of a period would need to change!


The best I can suggest is to use the PeriodType.days() to get the whole number of days between your DateTime objects and then calculate the fraction from the remainder:

DateTime start = new DateTime(1960, 3, 1, 9, 45, 0, 0);
DateTime end = new DateTime(2006, 3, 16, 9, 21, 0, 0);
Period wholeDays = new Period(start, end, PeriodType.days());

DateTime startRemainder = start.plus(wholeDays);
DateTime startRemPlusOne = startRemainder.plusDays(1);

Duration remainderDur = new Duration(startRemainder, end);
Duration wholeDayDur = new Duration(startRemainder, startRemPlusOne);

double fraction = remainderDur.getMillis() / wholeDayDur.getMillis();
fraction += wholeDays.getDays();

Stephen


Many thanks for any reply, hopefully even with a couple of lines of code.

Sincerely

Carl Henrik Janson



-------------------------------------------------------
This SF.Net email is sponsored by xPML, a groundbreaking scripting language that extends applications into web and mobile media. Attend the live webcast and join the prime developer group breaking into this new coding territory!
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=110944&bid=241720&dat=121642
_______________________________________________
Joda-interest mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/joda-interest



-------------------------------------------------------
This SF.Net email is sponsored by xPML, a groundbreaking scripting language
that extends applications into web and mobile media. Attend the live webcast
and join the prime developer group breaking into this new coding territory!
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=110944&bid=241720&dat=121642
_______________________________________________
Joda-interest mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/joda-interest

Reply via email to