JAVA EXAMPLE PROGRAMS

JAVA EXAMPLE PROGRAMS

Publish Your Article Here

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
When to use LinkedList or ArrayList?
Accessing elements are faster with ArrayList, because it is index based. But accessing is difficult with LinkedList. It is slow access. This is to access any element, you need to navigate through the elements one by one. But insertion and deletion is much faster with LinkedList, because if you know the node, just change the pointers before or after nodes. Insertion and deletion is slow with ArrayList, this is because, during these operations ArrayList need to adjust the indexes according to deletion or insetion if you are performing on middle indexes. Means, an ArrayList having 10 elements, if you are inserting at index 5, then you need to shift the indexes above 5 to one more.
Famous Quotations
Insanity: doing the same thing over and over again and expecting different results.
-- Albert Einstein

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 © 2022 by Nataraja Gootooru. All Rights Reserved.