Java Day Between timeDifferenceWithCurrentTime(Date startTime)

Here you can find the source of timeDifferenceWithCurrentTime(Date startTime)

Description

This Methods gives the result Milliseconds elapsed to Current Time Note : this method does not check for dates.

License

Open Source License

Parameter

Parameter Description
startTime a parameter

Declaration

public static long timeDifferenceWithCurrentTime(Date startTime) 

Method Source Code

//package com.java2s;
// distribute, sublicense, and/or sell copies of the Software, and to

import java.util.Calendar;
import java.util.Date;

public class Main {
    /**/*from ww  w.j a  v a 2  s  . c  om*/
     * This Methods gives the result Milliseconds elapsed to Current Time
     * Note : this method does not check for dates. It only works on time
     * Example : 
     * Input Time : 12 July 2011 12:20:80 AM
     * The input will be converted to current date and the time will be retained 
     * and this newly formed date will be compared with current date's time.  
     * @param startTime
     * @return
     */
    public static long timeDifferenceWithCurrentTime(Date startTime) {

        Date currentTime = getCurrentDateTime();
        Calendar startDateCal = Calendar.getInstance();
        startDateCal.setTime(startTime);

        Calendar newTime = Calendar.getInstance();
        newTime.set(Calendar.HOUR, startDateCal.get(Calendar.HOUR));
        newTime.set(Calendar.MINUTE, startDateCal.get(Calendar.MINUTE));
        newTime.set(Calendar.MILLISECOND, startDateCal.get(Calendar.MILLISECOND));

        return ((newTime.getTime()).getTime() - currentTime.getTime());
    }

    public static Date getCurrentDateTime() {
        Calendar cal = Calendar.getInstance();

        cal.set(Calendar.MILLISECOND, 0);
        return cal.getTime();
    }
}

Related

  1. monthDifference(final Date a, final Date b)
  2. monthsDiff(final Date data1, final Date data2)
  3. stdNewDate(String tzDiff)
  4. substractDate(final Date date, final Integer different)
  5. timeDifferenceInSeconds(Date startDate, Date endDate)
  6. truncateDifferMonths(Date date)
  7. yearDifference(Date date1, Date date2)
  8. yearTimeDiff(Date d1, Date d2)