

 Thread Tools  Search this Thread 
#1
 
 
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
 
 
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: AD<z <BC 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 [AD, BC]. So the term you seek is the sum of (min (B D + z)  Max (A, C + z)) For z integer in [AD, BC]. Hope this helps you, I think to understand this you should have a good knowledge about the mathematics. 
#3
 
 
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: Login: [A, B] 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
 
 
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
 
 
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 nonempty 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
 
 
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)) { clnd1.add(Calendar.DATE, 1); } clnd2.setTime(clnd1.getTime()); clnd2.set(Calendar.HOUR_OF_DAY, PlageFin); if (clnd2.getTime().before(clnd1.getTime())) { clnd2.add(Calendar.DATE, 1); } / / 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 clnd1.add(Calendar.DATE, 1); clnd2.add(Calendar.DATE, 1); } return intersection; } 

Tags: duration, java tools, javautil package, programming language, utilities 
Thread Tools  Search this Thread 

Similar Threads for: "Problem calculating duration in java"  
Thread  Thread Starter  Forum  Replies  Last Post 
Actual Duration Not Calculating Accurately  by76  Microsoft Project  1  27042011 06:23 AM 
Nokia c5 call duration problem  Tarank  Portable Devices  5  30102010 12:17 AM 
Problem calculating date  Gunner 1  Software Development  5  23022010 01:13 AM 
Calculating Age in java  Miles Runner  Software Development  5  03022010 12:54 AM 
Calculating frequency in RPM with JAVA  gvBlake  Software Development  2  23082008 06:02 PM 