How to get the difference between two dates in java in hours/minutes ?

4.38K viewsJava
3

How to get the difference between two dates in java in hours or minutes.

For Example  :

Date startDate = “05/10/2019 09:29:58”;

Date endDate = “031/12/2019 10:31:48”;

Code return hours or minutes based on input

Anonymous Answered question June 17, 2019
Add a Comment
1

I hope the snippet code provided is sufficient for this problem, please try…!!!

public long getHrsOrMinutesDiff(Date fromDate, Date toDate, String returnType) {
		
		if ("hour".equals(returnType)) { // here if pass hours the method will return hours between the dates
			long diff = fromDate.getTime() - toDate.getTime();
			return diff / (60 * 60 * 1000);
		} else {// here if pass minutes the method will return minutes between the dates
			long diff = fromDate.getTime() - toDate.getTime();
			return diff / (60 * 1000);
		}
		
}

 

Manohar Changed status to publish June 17, 2019
Add a Comment
You are viewing 1 out of 2 answers, click here to view all answers.
Write your answer.