Latest Tutorials| Questions and Answers|Ask Questions?|Site Map

Home Java Beginners Date Difference


Share on Google+Share on Google+

Date Difference

This example learns how to make the difference between two dates and how to convert milliseconds into seconds, seconds into minutes, minutes into hours, hours into days.

Date Difference


This example learns how to make the difference between two dates and how to convert milliseconds into seconds, seconds into minutes, minutes into hours, hours into days.

Here we are using Calendar, an abstract base class that extends Object class and makes a difference between a Date object and a set of integer fields. Calendar class provides a getInstance()  method for returning a Calendar object whose time fields have been initialized with the current date and time.

The methods used:
setTimeInMillis(long millis): This method is used to set current time in calendar object.

getInstance(): This method is used to get a calendar using the default time zone, locale and current time.

The code of the program is given below:

import java.util.Calendar;
public class DateDifferent{  
  public static void main(String[] args){
  Calendar calendar1 = Calendar.getInstance();
  Calendar calendar2 = Calendar.getInstance();
  long milliseconds1 = calendar1.getTimeInMillis();
  long milliseconds2 = calendar2.getTimeInMillis();
  long diff = milliseconds2 - milliseconds1;
  long diffSeconds = diff / 1000;
  long diffMinutes = diff / (60 1000);
  long diffHours = diff / (60 60 1000);
  long diffDays = diff / (24 60 60 1000);
  System.out.println("\nThe Date Different Example");
  System.out.println("Time in milliseconds: " + diff
" milliseconds.");
  System.out.println("Time in seconds: " + diffSeconds
" seconds.");
  System.out.println("Time in minutes: " + diffMinutes 
" minutes.");
  System.out.println("Time in hours: " + diffHours 
" hours.");
  System.out.println("Time in days: " + diffDays 
" days.");

The output of the program is given below:

C:\rajesh\kodejava>java DateDifferent
The Date Different Example
Time in milliseconds: 14860800000 milliseconds.
Time in seconds: 14860800 seconds.
Time in minutes: 247680 minutes.
Time in hours: 4128 hours.
Time in days: 172 days.

Download this example.


If you enjoyed this post then why not add us on Google+? Add us to your Circles

Liked it!  Share this Tutorial

Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: July 6, 2007

Related Tutorials

Discuss: Date Difference   View All Comments

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
February 23, 2012
Thank you

saved me the time of developing an algorithm
Arun R T
April 3, 2012
Date Difference Problem

In your code import java.util.Calendar; public class DateDifferent{ public static void main(String[] args){ Calendar calendar1 = Calendar.getInstance(); Calendar calendar2 = Calendar.getInstance(); calendar1.set(2007, 01, 10); calendar2.set(2007, 07, 01); long milliseconds1 = calendar1.getTimeInMillis(); long milliseconds2 = calendar2.getTimeInMillis(); long diff = milliseconds2 - milliseconds1; long diffSeconds = diff / 1000; long diffMinutes = diff / (60 * 1000); long diffHours = diff / (60 * 60 * 1000); long diffDays = diff / (24 * 60 * 60 * 1000); System.out.println("\nThe Date Different Example"); System.out.println("Time in milliseconds: " + diff + " milliseconds."); System.out.println("Time in seconds: " + diffSeconds + " seconds."); System.out.println("Time in minutes: " + diffMinutes + " minutes."); System.out.println("Time in hours: " + diffHours + " hours."); System.out.println("Time in days: " + diffDays + " days."); } } I substitute calendar1.set(2012, 03, 31); calendar2.set(2012, 04, 03); Time in days: 2 days. "But current is 3"
May 1, 2012
Does not work

It does not work correctly from month to month!
September 5, 2012
what is the modification required?

long diff = milliseconds2 - milliseconds1; but it gets the difference based on 12 hr clock so it returns negative when send time is prior to 1st time while calculating the difference. so what is the modification required in order to get proper time difference value without negative value.