Calculating the time difference between two time stamp object - java

Kar*_*azz 3 java time timestamp date

I am having java.sql.date and java.sql.time objects, I need to find the time duration between the dates.

So i am creating java.sql.timestamp object by using above date and time object

  Timestamp timestamp1 = new Timestamp(StartDate.getYear(),
        StartDate.getMonth(), StartDate.getDay(),
        StartTime.getHours(), StartTime.getMinutes(), 00,
        00);
Run Code Online (Sandbox Code Playgroud)

Here is mycode

String date = "2010-01-05";
    SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
    java.util.Date date3 = null;
    try {
        date3 = sdf1.parse(date);
    } catch (ParseException e) {
        e.printStackTrace();
    }
    Date StartDate = new Date(date3.getTime());
    System.out.println("Date " + StartDate);

    String date2 = "2010-01-06";
    java.util.Date date4 = null;
    try {
        date4 = sdf1.parse(date2);
    } catch (ParseException exception) {
        exception.printStackTrace();
    }
    Date EndDate = new Date(date4.getTime());
    System.out.println("Date " + EndDate);

    String time = "01:00";
    DateFormat formatter = new SimpleDateFormat("HH:mm");
    java.sql.Time StartTime = null;
    try {
        StartTime = new java.sql.Time(formatter.parse(time).getTime());
    } catch (ParseException exception2) {
        exception2.printStackTrace();
    }
    System.out.println("TIMEEEEEEEEEE====" + StartTime);

    String time2 = "02:00";
    java.sql.Time EndTime = null;
    try {
        EndTime = new java.sql.Time(formatter.parse(time2).getTime());
    } catch (ParseException exception3) {
        exception3.printStackTrace();
    }
    System.out.println("TIMEEEEEEEEEE====" + EndTime);


    Timestamp timestamp1 = new Timestamp(StartDate.getYear(),
            StartDate.getMonth(), StartDate.getDay(),
            StartTime.getHours(), StartTime.getMinutes(), 00,
            00);
    Timestamp timestamp2 = new Timestamp(EndDate.getYear(),
            EndDate.getMonth(), EndDate.getDay(),
            EndTime.getHours(), EndTime.getMinutes(), 00, 00);

    long milliseconds = timestamp2.getTime() - timestamp1.getTime();
    int seconds = (int) milliseconds / 1000;

    // calculate hours minutes and seconds
    int hours = seconds / 3600;
    int minutes = (seconds % 3600) / 60;
    seconds = (seconds % 3600) % 60;
    System.out.println(hours+"h:"+minutes+"m:"+"00s");
Run Code Online (Sandbox Code Playgroud)

Test case

when I give date as 2010-01-05 and date2 as 2010-01-06 I am getting output as below

Date 2010-01-05
Date 2010-01-06
TIMEEEEEEEEEE====01:00:00
TIMEEEEEEEEEE====02:00:00
25h:0m:00s
Run Code Online (Sandbox Code Playgroud)

when I give date as 2010-01-05 and date2 as 2010-01-11 I am getting output in negative value as below

Date 2010-01-05
Date 2010-01-11
TIMEEEEEEEEEE====01:00:00
TIMEEEEEEEEEE====02:00:00
-23h:0m:00s
Run Code Online (Sandbox Code Playgroud)

Help me to correct if I am doing something wrong. Thanks in advance.

小智 6

Manual time calculation:-

Converts Date in milliseconds (ms) and calculate the differences between two dates, with following rules :

  • 1000 milliseconds = 1 second
  • 60 seconds = 1 minute
  • 60 minutes = 1 hour
  • 24 hours = 1 day

Sample Example:-

package com.dps2.practice.dyuti;

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

public class DateDifferentExample {

    public static void main(String[] args) {

        String dateStart = "08/11/2016 09:29:58";
        String dateStop = "08/12/2016 10:31:48";

        //HH converts hour in 24 hours format (0-23), day calculation
        SimpleDateFormat format = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");

        Date d1 = null;
        Date d2 = null;

        try {
            d1 = format.parse(dateStart);
            d2 = format.parse(dateStop);

            //in milliseconds
            long diff = d2.getTime() - d1.getTime();

            long diffSeconds = diff / 1000 % 60;
            long diffMinutes = diff / (60 * 1000) % 60;
            long diffHours = diff / (60 * 60 * 1000) % 24;
            long diffDays = diff / (24 * 60 * 60 * 1000);

            System.out.print(diffDays + " days, ");
            System.out.print(diffHours + " hours, ");
            System.out.print(diffMinutes + " minutes, ");
            System.out.print(diffSeconds + " seconds.");

        } catch (Exception e) {
            e.printStackTrace();
        }

    }

}
Run Code Online (Sandbox Code Playgroud)