date difference calculation  [ 688 views ]

Goal: get the time difference between two dates

The following code gives the difference between two times in this form: 1h 12m

  public static String getTimeDifference(Date Start, Date End){
    String ret = "";

    //in milliseconds
    long diff = End.getTime() - Start.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);

    if(diffHours >0 ){ ret = diffHours + "h "; }
    ret += diffMinutes + "m";

    return ret;
  }

The seconds and the days not used. Just good to know how can calculate these values.

#sidebar a { color:#fff; } #sidebar ul ul li { color: #DEF585; } #sidebar h2 { color: #fff; } #sidebar ul p, #sidebar ul select { color: #BEDDBE; } #backfly { background: url(images/golfBallWallPaper.jpg) left bottom fixed repeat-x #65a51d; }