Get the average of two java.util.Date

I have an array of java.util.Date objects. I'm trying to find a mean.

For example, if I have 2 date objects between 7:40 a.m. and 7:50 a.m. I have to get the middle date object at 7:45 a.m.

The approach I'm thinking of is ineffective:

  • for a cycle through all dates
  • find the difference between 0000 and time
  • add this time interval to the full
  • divide it by the total number
  • convert this time to a date object

Is there a simpler function to do this?

+6
source share
6 answers

Basically, you can simply add the β€œmillions from the Unix era” of all Date objects and find the average of them. Now a complex bit avoids overflow. Possible options:

  • Divide by a known amount (e.g. 1000) to avoid overflow; this reduces accuracy by a known amount (in this case, by a second), but will fail if you have more than 1000 elements.
  • Divide each millis value by the number of dates you average; it will always work, but has a difficult to understand decrease in accuracy
  • Use BigInteger instead

Approach Example 1:

 long totalSeconds = 0L; for (Date date : dates) { totalSeconds += date.getTime() / 1000L; } long averageSeconds = totalSeconds / dates.size(); Date averageDate = new Date(averageSeconds * 1000L); 

Approach Example 3:

 BigInteger total = BigInteger.ZERO; for (Date date : dates) { total = total.add(BigInteger.valueOf(date.getTime())); } BigInteger averageMillis = total.divide(BigInteger.valueOf(dates.size())); Date averageDate = new Date(averageMillis.longValue()); 
+20
source

With a large number of dates, taking the sum of all dates together will certainly go into overflow. If you want you to do it like this (in pseudocode):

 var first = dates.getFirst var sumOfDifferences = 0 loop over all dates for each date sumOfDifferences += date - first var averageDate = first + sumOfDifferences/countOfDates 

It will never make you work when overflowing.

+1
source

There is already an answer here: Summarize two dates in Java

You just need to summarize all your date objects using getTime() , and then divide it by the number of objects and convert it back to a Date object. Done.

0
source

To avoid overflow in average time caluclation:

sort by date; save the value of the first date in time0 ;

Calculate the average deltaTimes by subtracting g for the first time0 from all times. Then summarize and divide.

Result = new Date(time0 + avgDeltas) ;

0
source

Try it. Here, every day is converted to the long value my getTime() . This will return the mil-second value. Then we can continue.

  SimpleDateFormat sdf = new SimpleDateFormat("HH:mma"); Date date1=sdf.parse("7:40AM"); Date date2=sdf.parse("7:50AM"); long date1InMilSec=date1.getTime(); long date2InMilSec=date2.getTime(); System.out.println("Average "+sdf.format((date1InMilSec+date2InMilSec)/2)); 
0
source

After publishing java 8 you can use

 Date avgDate = new Date( LongStream.of(datesAsLongArray).sum() / datesAsLongArray.length * 1000); 
0
source

Source: https://habr.com/ru/post/951577/


All Articles