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
Inner class and Anonymous class
Inner class: classes defined in other classes, including those defined in methods are called inner classes. An inner class can have any accessibility including private.

Anonymous class: Anonymous class is a class defined inside a method without a name and is instantiated and declared in the same place and cannot have explicit constructors.
Famous Quotations
The very best thing you can do for the whole world is to make the most of yourself.
-- Wallace Wattles

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.