Time Difference Between Two Timestamps In Java

Chapter: Date and Time Last Updated: 22-09-2018 08:30:15 UTC

Program:

            /* ............... START ............... */
                
import java.sql.Timestamp;
import java.util.Calendar;

public class JavaTimeDifference {
	public static void main(String[] args) {

		java.util.Date date = new java.util.Date();
		Timestamp timestamp1 = new Timestamp(date.getTime());

		Calendar cal = Calendar.getInstance();
		cal.setTimeInMillis(timestamp1.getTime());

		// add a bunch of seconds to the calendar
		cal.add(Calendar.SECOND, 98765);

		// create a second time stamp
		Timestamp timestamp2 = new Timestamp(cal.getTime().getTime());

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

		int hours = seconds / 3600;
		int minutes = (seconds % 3600) / 60;
		seconds = (seconds % 3600) % 60;

		System.out.println("timestamp1: " + timestamp1);
		System.out.println("timestamp2: " + timestamp2);

		System.out.println("Difference: ");
		System.out.println(" Hours: " + hours);
		System.out.println(" Minutes: " + minutes);
		System.out.println(" Seconds: " + seconds);
	}
}
                /* ............... END ............... */
        

Output

timestamp1: 2016-04-26 20:18:53.548
timestamp2: 2016-04-27 23:44:58.548
Difference: 
 Hours: 27
 Minutes: 26
 Seconds: 5

Tags

Time Difference Between Two Timestamps,java timestamp difference in minutes, java timestamp difference in milliseconds,java timestamp differences, calculate time difference in java

Similar Programs Chapter Last Updated
Java Program to Calculate Time Difference In Hours Date and Time 20-09-2023
Java Code To Calculate Time Difference In Milliseconds Date and Time 20-09-2023
Java Program To Calculate Time Difference In Seconds Date and Time 20-09-2023
How to calculate time difference between two time zones in Java Date and Time 20-09-2023
Java Date And Time API To Create A Custom Calendar Date and Time 20-09-2023
How To Format Date And Time In Java Date and Time 16-09-2023
How Do You Get The First Day Of The Week In Java Date and Time 15-09-2023
Java Code To Change Date Format Date and Time 15-08-2023
Java Program To Find Day Of The Week For A Given Date Date and Time 10-08-2023
Java code to check if date is weekend Date and Time 29-06-2023
Java program to find number of days between two dates Date and Time 29-06-2023
Java Program To Get The Current Time In Different Time Zones Date and Time 22-06-2023
How To Add Working Days To A Date In Java Date and Time 22-06-2023
Java Program To Add A Specified Number Of Days To A Given Date Date and Time 22-06-2023
Write A Program To Check Leap Year In Java Date and Time 22-06-2023
Write A Program To Display Current Date And Time In Java Date and Time 22-06-2023
Java Date Format AM PM Date and Time 14-05-2023
Java Add Months To Date Date and Time 14-05-2023
Add Days To Date In Java example Date and Time 14-05-2023
Java Program To Calculate Days Between Two Dates Date and Time 16-04-2023
Java Program To Add Days To Date Date and Time 16-04-2023
How To Find Difference Between Two Dates In Java Date and Time 01-04-2023
Add Days To Date Java Example Date and Time 08-08-2021

1