Java Program to Calculate Difference Between Two Time Periods

Configurare noua (How To)

Situatie

SimpleDateFormat class has been added to the java.text package in the 7th JDK version. Parse the Time Period in the format HH:MM:SS by creating a SimpleDateFormat object. SimpleDateFormat object parses the Time period and returns a Date object which can be used to calculate the time elapsed.

Solutie

Pasi de urmat

// Java Program to Find the difference
// between Two Time Periods

// Importing the Date Class from the util package
import java.util.*;

// Importing the SimpleDateFormat
// Class from the text package
import java.text.*;

public class GFG {

public static void main(String[] args) throws Exception
{

// Dates to be parsed
String time1 = “18:00:00”;
String time2 = “7:30:50”;

// Creating a SimpleDateFormat object
// to parse time in the format HH:MM:SS
SimpleDateFormat simpleDateFormat
= new SimpleDateFormat(“HH:mm:ss”);

// Parsing the Time Period
Date date1 = simpleDateFormat.parse(time1);
Date date2 = simpleDateFormat.parse(time2);

// Calculating the difference in milliseconds
long differenceInMilliSeconds
= Math.abs(date2.getTime() – date1.getTime());

// Calculating the difference in Hours
long differenceInHours
= (differenceInMilliSeconds / (60 * 60 * 1000))
% 24;

// Calculating the difference in Minutes
long differenceInMinutes
= (differenceInMilliSeconds / (60 * 1000)) % 60;

// Calculating the difference in Seconds
long differenceInSeconds
= (differenceInMilliSeconds / 1000) % 60;

// Printing the answer
System.out.println(
“Difference is ” + differenceInHours + ” hours ”
+ differenceInMinutes + ” minutes ”
+ differenceInSeconds + ” Seconds. “);
}
}

Output:

Difference is 10 hours 29 minutes 10 Seconds.

Tip solutie

Permanent

Voteaza

(5 din 15 persoane apreciaza acest articol)

Despre Autor

Leave A Comment?