# Thread: Problem calculating duration in java

1. Member
Join Date
Dec 2009
Posts
296

## Problem calculating duration in java

Hello,
I am facing a problem that seems quite simple, but I can not solve, at least simply. I try to calculate, for an event lasting 24 hours (but can be mounted on two calendar days), time spent during a certain time slot (which can be mounted on 2 days) and is repeated every day.
For example:
The event is a connection defined by its start date and end date.
The night time slot is: 22h00 - 06h00
-> How to calculate the time of the night connection?
If the connection lasts from 05:00 until 02:00 the next day, it must refer 5 hours (1 hour and 4 start to finish). I know about using the Date / Calendar. In short I am having problem in calculating the duration. If any one have an idea about this then please help me.

2. Member
Join Date
Nov 2009
Posts
333

## Re: Problem calculating duration in java

Hello,
Time of connection: interval [A, B].
Beaches Night: union of intervals [C + z, D + z] Where z is an integer and C <D. Looking all x belonging to the intersection of these two sets.
For any x in the intersection, there exists z integer such that:
A <x <B and C + z<x <D + z
Then:
A <D + z and C + z<B
So:
A-D<z <B-C
Let [X, Y] interval.
Then:
[A, B] intersection [X, Y] = [max (A, X), min (B, Y)]
I conclude that the intersection of the connection and the beaches at night is the union of [max (A, C + z), Min (B D + z)] For z integer in [A-D, B-C].
So the term you seek is the sum of (min (B D + z) - Max (A, C + z)) For z integer in [A-D, B-C].
Hope this helps you, I think to understand this you should have a good knowledge about the mathematics.

3. Member
Join Date
Nov 2009
Posts
518

## Re: Problem calculating duration in java

Hello,
A small addition to the above. If your beaches were irregular rather than repeat all day (note the [CnDn]), We have:
Beaches: union over n of [CnDn]
Intersection of the connection and beaches:
intersection of [A, B] and (union over n of [CnDn])
= N on the Common (intersection of [A, B] and [CnDn])
= The union over n [max (A, Cn), Min (B, Dn)]
And therefore the common term:
sum over n of ((min (B, Dn) - Max (A, Cn)) If the difference is positive)
(if the difference is negative, the interval is empty!)

4. Member
Join Date
Dec 2009
Posts
296

## Re: Problem calculating duration in java

Hello,
In any case, beautiful mathematical proof: here reminds me of my youth! Your formula:
sum over n of ((min (B, Dn) - max (A, Cn)) if the difference is positive)
(if the difference is negative, the interval is empty)
Now it is at the algo "Java" that I have a problem here. In particular with my time slots which are rather of the type [C + z * 24h; D + z * 24h]

5. Member
Join Date
Nov 2009
Posts
343

## Re: Problem calculating duration in java

Hello,
For the above mentioned posts I would like to add some more in it. In the case of your regular beaches, it has an infinite number of beaches but they are well known and therefore can reduce the number of elements to sum by calculating in which case the difference is negative (ie non-empty interval) is calculation that I performed to deduce that the only z to take into account in the calculation are z in [A / D, B / C].

6. Member
Join Date
Nov 2009
Posts
446

## Re: Problem calculating duration in java

Hello,
Please have a look at the following example, it is just a sample code for you to understand.
Code:
```static Public long calculation(EventDebut Date, Date eventFin, int plageDebut, int plageFin) {

GregorianCalendar clnd1 = new GregorianCalendar(); / / Novice
GregorianCalendar clnd2 = new GregorianCalendar(); / / End range

long intersection = 0;

/ / It defines the time slot that starts before beginning
clnd1.setTime(eventDebut);
clnd1.set(Calendar.HOUR_OF_DAY, PlageDebut);
clnd1.set(Calendar.MINUTE, 0);
clnd1.set(Calendar.SECOND, 0);
clnd1.set(Calendar.Millisecond, 0);
if (clnd1.getTime().after(eventDebut)) {
}

clnd2.setTime(clnd1.getTime());
clnd2.set(Calendar.HOUR_OF_DAY, PlageFin);
if (clnd2.getTime().before(clnd1.getTime())) {
}

/ / As long as the slot is not later than end:
while (clnd1.getTime().before(eventFin)) {

+ intersection = Math.max(Math.min(eventFin.getTime(), clnd2.getTimeInMillis()) - Math.max(eventDebut.getTime(), Cal1.getTimeInMillis()), 0);

/ / It shifts the range of 24h