JAVA EXAMPLE PROGRAMS

JAVA EXAMPLE PROGRAMS

Checkout for Promo Codes

Program: How to calculate difference between two dates in java?


Below example shows how to calculate diffence between two dates by seconds or by minutes or by hours or by days.

package com.java2novice.date;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class MyDateDifferenceExamples {

	public static void main(String a[]){
		
		String startDate = "22/02/2014 12:30:00";
		String endDate = "24/02/2014 12:30:00";
		
		/**
		 * SimpleDateFormat converts string format to date object
		 */
		SimpleDateFormat sdFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
		try {
			Date startDateObj = sdFormat.parse(startDate);
			Date endDateObj = sdFormat.parse(endDate);
			// startDateObj.getTime() method gives date in milli seconds format
			System.out.println("Time in milli seconds: "+startDateObj.getTime());
			
			// find time difference in milli seconds
			long timeDiff = endDateObj.getTime() - startDateObj.getTime();
			System.out.println("Time difference in Milli seconds: "+timeDiff);
			
			// time difference in seconds
			long secondsDiff = (timeDiff/1000);
			System.out.println("Time difference in seconds: "+secondsDiff);
			
			// time difference in minutes
			long minDiff = timeDiff/(1000*60);
			System.out.println("Time difference in minutes: "+minDiff);
			
			// time difference in minutes
			long hoursDiff = timeDiff/(1000*60*60);
			System.out.println("Time difference in hours: "+hoursDiff);
			
			// time difference in minutes
			long daysDiff = timeDiff/(1000*60*60*24);
			System.out.println("Time difference in days: "+daysDiff);
			
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}

Output:
Time in milli seconds: 1393052400000
Time difference in Milli seconds: 172800000
Time difference in seconds: 172800
Time difference in minutes: 2880
Time difference in hours: 48
Time difference in days: 2
<< Previous Program 

List Of All Java Date Programs:

  1. Java Date basic functions.
  2. How to create a date with milli seconds?
  3. How to check if the date is before the specified date?
  4. How to check if the date is after the specified date?
  5. How to get current time in milli seconds?
  6. How to format date into user define format?
  7. How to calculate difference between two dates in java?
Knowledge Centre
Interface and its usage
Interface is similar to a class which may contain method's signature only but not bodies and it is a formal set of method and constant declarations that must be defined by the class that implements it. Interfaces are useful for declaring methods that one or more classes are expected to implement, capturing similarities between unrelated classes without forcing a class relationship and determining an object's programming interface without revealing the actual body of the class.
Famous Quotations
Be yourself; everyone else is already taken.
-- Oscar Wilde

About Author

I'm Nataraja Gootooru, programmer by profession and passionate about technologies. All examples given here are as simple as possible to help beginners. The source code is compiled and tested in my dev environment.

If you come across any mistakes or bugs, please email me to [email protected].

Most Visited Pages

Other Interesting Sites

Reference: Java™ Platform Standard Ed. 7 - API Specification | Java™ Platform Standard Ed. 8 - API Specification | Java is registered trademark of Oracle.
Privacy Policy | Copyright © 2020 by Nataraja Gootooru. All Rights Reserved.