Search code examples
javamongodb-java

How to find the difference between times



I am trying to make a program for to find the difference between two hours. The program searching in to the database of mongoDB all the hours and it must find every time the difference between them. But I do not know how to give the second value to take it every time in the loop.

Until now i make to find one time and to subtract from one standar.

How i can put to searching for the first two values of time in a loop...every time.

Thank you very much for your help!!!

    int count = 1;
    int start = 1;

    while (cursorEvents.hasNext()) {

        DBObject documentInEventCollection = cursorEvents.next();


        if("pageLoad".equals(documentInEventCollection.get("type"))){ 

            System.out.println("URL(" + count + "): " + documentInEventCollection.get("url").toString());
            System.out.println("time-start(" + start + "): " + documentInEventCollection.get("timeStamp").toString());
            count++;
            start++;

    try {
    String timeStart = (documentInEventCollection.get("timeStamp").toString());

    String timeStop = ("2015-07-24;12:26:54");

    SimpleDateFormat format = new SimpleDateFormat("yyyy-dd-MM;HH:mm:ss");

    Date d1 = null;
    Date d2 = null;


        d1 = format.parse(timeStart);
        d2 = format.parse(timeStop);

        //in milliseconds
        long diff = d2.getTime() - d1.getTime();

        long diffSeconds = diff / 1000 % 60;
        long diffMinutes = diff / (60 * 1000) % 60;
        long diffHours = diff / (60 * 60 * 1000) % 24;
        long diffDays = diff / (24 * 60 * 60 * 1000);

        System.out.print(diffDays + " days, ");
        System.out.print(diffHours + " hours, ");
        System.out.print(diffMinutes + " minutes, ");
        System.out.print(diffSeconds + " seconds.");

    } catch (Exception e) {
        e.printStackTrace();
    }
   }            
   }    

Solution

  • I use follow solution to calculate a time different:

    //Compute the time diff
    Map<TimeUnit, Long> deltaTime = computeDiff(now.getTime(), target.getTime());
    
    //Get the specific values            
    long d = deltaTime.get(TimeUnit.DAYS);
    long m = deltaTime.get(TimeUnit.MINUTES); 
    long s = deltaTime.get(TimeUnit.SECONDS);
    

    Methode code:

     private static Map<TimeUnit, Long> computeDiff(Date start, Date end) {
                long diffMilTime = end.getTime() - start.getTime();
                List<TimeUnit> units = new ArrayList<>(EnumSet.allOf(TimeUnit.class));
                Collections.reverse(units);
    
                Map<TimeUnit, Long> result = new LinkedHashMap<>();
                long restMilTime = diffMilTime;
                for (TimeUnit unit : units) {
                    long diff = unit.convert(restMilTime, TimeUnit.MILLISECONDS);
                    long diffInMilliesForUnit = unit.toMillis(diff);
                    restMilTime = restMilTime - diffInMilliesForUnit;
                    result.put(unit, diff);
                }
                return result;
            }