With monday.comâ€™s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

Hello Experts!

I'm creating a subscription-based calculator in Java and I'm trying to figure out how to correctly prorate the value if the subscription.

My yearly subscription fee is a flat $120 per year (365 days). If a user wants only a 6 month subscription then the cost is $60. But let's say the subscriber signs on Jan. 15, 2011 and ends his subscription on June 30. That's only 5 months and 15 days. I need a formula that can correctly calculate the amount I need to charge. In this case it would be ~$55.00. It gets a little trickier if my user signs on Feb. 6th and ends their subscription Dec. 28.

Can anyone help?

I'm creating a subscription-based calculator in Java and I'm trying to figure out how to correctly prorate the value if the subscription.

My yearly subscription fee is a flat $120 per year (365 days). If a user wants only a 6 month subscription then the cost is $60. But let's say the subscriber signs on Jan. 15, 2011 and ends his subscription on June 30. That's only 5 months and 15 days. I need a formula that can correctly calculate the amount I need to charge. In this case it would be ~$55.00. It gets a little trickier if my user signs on Feb. 6th and ends their subscription Dec. 28.

Can anyone help?

Obviously the amount per day is fuzzy due to leap years, so one month in July could cost just fractionally less on a leap year.

days in year = 1/1/next - 1/1/current

days subscribed = last - first + 1

amount to pay = days subscribed / days in year * $120

http://technojeeves.com/joomla/index.php/free/71-difference-between-two-dates-in-java

```
public static double getSub(Date d1, Date d2) {
final double PER_YEAR = 120;
long days = TimeDiff.getTimeDifference(d1, d2, TimeDiff.TimeField.DAY);
return (double)(days * PER_YEAR / 365);
}
```

last - first

Otherwise, you will never get the full year in 1/1 - 31/12, you will be short 1 day

Not only that, a subscription for 7/7-7/7 will result in 0 days, when common sense says 1?

http://helpdesk.objects.com.au/java/how-to-calculate-the-difference-between-two-dates-or-times

days * PER_YEAR / 365

That would be:

29 *120 / 365 = 9.53

I guess for situations like this I would have to write a separate condition to calculate something like this?

Start date: 2/15/2010

End date: 3/30/2010

I first have to calculate the number of months then calculate the number of days?

If you are not going by daily rate, then a stepped calculation may work better

Is full year? $120

Is start = end? $120 / days in year (minimum of 1 day)

else:

d = start

months = 0

days = 0

while d < end {

add 1 month to d

months++

}

while d < end {

add 1 day to d

days++

}

calc = (months + days / 30.0) * $120 / 12

Notes:

It is possible to have 3 months 30 days which is 1 day less than 4 months in some cases, but you will charge 4 months exactly.

It is also possible to charge 27 days in feb (27/28) at 27/30 of the monthly rate.

Give some take some.

All Courses

From novice to tech pro — start learning today.

and you can take your start and end date and increment the month of the start until they are both in the same month

http://helpdesk.objects.com.au/java/how-do-i-add-a-specified-number-of-months-to-a-java-date

then calculate the #days as I posted above