Difference between two dates in days, hours, minutes, seconds, milliSeconds- where dates are manually created using dd-MM-yyyy and dd-MM-yyyy hh:mm:ss format in java


Contents of page :
  • Program 1) Difference between two dates in days, hours, minutes, seconds, milliSeconds- where dates are manually created using dd-MM-yyyy format

  • Program 2) Difference between two dates in days, hours, minutes, seconds, milliSeconds- where dates are manually created using dd-MM-yyyy hh:mm:ss format



Program 1) Difference between two dates in days, hours, minutes, seconds, milliSeconds- where dates are manually created using dd-MM-yyyy format

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.GregorianCalendar;
/** Copyright (c), AnkitMittal JavaMadeSoEasy.com */
public class DateTest {
   public static void main(String... a) {
          //1. Forming manual date
          Date date1 = null;
          Date date2 = null;
          try {
                 SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
                 date1 = sdf.parse("25-02-2015");
                 date2 = sdf.parse("23-02-2015");
          } catch (ParseException pe) {
                 pe.printStackTrace();
          }
          //2. FormingGregorianCalendar from manual date
          GregorianCalendar cal1 = new GregorianCalendar();
          GregorianCalendar cal2 = new GregorianCalendar();
          cal1.setTime(date1);
          cal2.setTime(date2);
         
          //----------------------------Now, starts actual LOGIC------------------
         
         
         //3. LOGIC for finding difference in milliSeconds, seconds, minutes, hours and days.
         
          /*
          * abs have been used to avoid negative results
          * Example - cal2.getTimeInMillis() - cal1.getTimeInMillis() would have produced negative result.
          */
          long diffIn_MilliSec = Math.abs(cal1.getTimeInMillis() - cal2.getTimeInMillis());
          System.out.println("Difference in milliSeconds = "+ diffIn_MilliSec);
          //172800000 milliSeconds = 2 day, 0 hour, 0 minute, 0 second, 0 milliSeconds
         
          System.out.println("Difference in seconds = "+ diffIn_MilliSec / (1000));
          //172800 seconds = 2 day, 0 hour, 0 minute, 0 second
         
          System.out.println("Difference in minutes = "+ diffIn_MilliSec / (1000 * 60));
          //2880 minutes = 2 day, 0 hour, 0 minute
         
          System.out.println("Difference in hours = "+ diffIn_MilliSec / (1000 * 60 * 60));
          //48 hours = 2 day, 0 hour
         
          System.out.println("Difference in days = "+ diffIn_MilliSec / (1000 * 60 *60 *24));
          //2 days
         
   }
}
/*OUTPUT
Difference in milliSeconds = 172800000
Difference in seconds = 172800
Difference in minutes = 2880
Difference in hours = 48
Difference in days = 2
*/




Program 2) Difference between two dates in days, hours, minutes, seconds, milliSeconds- where dates are manually created using dd-MM-yyyy hh:mm:ss format

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.GregorianCalendar;
/** Copyright (c), AnkitMittal JavaMadeSoEasy.com */
public class DateTest {
   public static void main(String... a) {
          //1. Forming manual date
          Date date1 = null;
          Date date2 = null;
          try {
                 SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy hh:mm:ss");
                 date1 = sdf.parse("25-02-2015 02:02:02");
                 date2 = sdf.parse("23-02-2015 01:01:01");
          } catch (ParseException pe) {
                 pe.printStackTrace();
          }
          //2. Forming GregorianCalendar from manual date
          GregorianCalendar cal1 = new GregorianCalendar();
          GregorianCalendar cal2 = new GregorianCalendar();
          cal1.setTime(date1);
          cal2.setTime(date2);
         
          //----------------------------Now, starts actual LOGIC------------------
         
         
            //3. LOGIC for finding difference in milliSeconds, seconds, minutes, hours and days.
         
          long diffIn_MilliSec = cal1.getTimeInMillis() - cal2.getTimeInMillis();
          System.out.println("Difference in milliSeconds = "+ diffIn_MilliSec);
          //176461000 milliSeconds = 2 day, 1 hour, 1 minute, 1 second, 0 milliSeconds
         
          System.out.println("Difference in seconds = "+ diffIn_MilliSec / (1000));
          //176461 seconds = 2 day, 1 hour, 1 minute, 1 second
         
          System.out.println("Difference in minutes = "+ diffIn_MilliSec / (1000 * 60));
          //2941 minutes = 2 day, 1 hour, 1 minute
         
          System.out.println("Difference in hours = "+ diffIn_MilliSec / (1000 * 60 * 60));
          //49 hours = 2 day, 1 hour
         
          System.out.println("Difference in days = "+ diffIn_MilliSec / (1000 * 60 * 60 * 24));
          //2 days
         
   }
}
/*OUTPUT
Difference in milliSeconds = 176461000
Difference in seconds = 176461
Difference in minutes = 2941
Difference in hours = 49
Difference in days = 2
*/



RELATED LINKS>
Creating Date >

Creating Date in java using Calendar, GregorianCalendar and java.util.Date | Getting Year, Month, Date, Hour, Minute, Second from current date


Convert Calendar and GregorianCalendar to Date | Convert Date to Calendar and GregorianCalendar in java




Difference between two Dates>

Check whether a given date lies between other two dates


2 approaches to check whether difference between given two dates is greater than specified number of days or not


Must read for you :

No comments:

Post a Comment