Program Discussion :: Basics
19 / 279
Write a program to accept two time stamps in the given format HH:MM:SS using apropriate datatypes and find the difference between the time stamps in seconds.
Answer:
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. ");
}
}
Asked In ::
Language:
Dutta
23 Jul, 2022 11:40 AM
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. ");
}
}
Language:
Dutta
23 Jul, 2022 11:40 AM
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. ");
}
}
Language:
Dutta
23 Jul, 2022 11:40 AM
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. ");
}
}
Language:
Dutta
23 Jul, 2022 11:40 AM
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. ");
}
}
Language:
Dutta
23 Jul, 2022 11:40 AM
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. ");
}
}
Language:
Dutta
23 Jul, 2022 11:40 AM
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. ");
}
}
Language:
Dutta
23 Jul, 2022 11:40 AM
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. ");
}
}
Language:
Dutta
23 Jul, 2022 11:40 AM
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. ");
}
}
Language:
Dutta
23 Jul, 2022 11:41 AM
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. ");
}
}
Language:
Dutta
23 Jul, 2022 11:41 AM
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. ");
}
}
Language:
Dutta
23 Jul, 2022 11:41 AM
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. ");
}
}
Language:
Dutta
23 Jul, 2022 11:41 AM
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. ");
}
}
Language:
Dutta
23 Jul, 2022 11:41 AM
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. ");
}
}
Language:
Dutta
23 Jul, 2022 11:41 AM
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. ");
}
}
Language:
Dutta
23 Jul, 2022 11:41 AM
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. ");
}
}
Language:
Dutta
23 Jul, 2022 11:41 AM
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. ");
}
}
Language:
Dutta
23 Jul, 2022 11:41 AM
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. ");
}
}
Language:
Dutta
23 Jul, 2022 11:41 AM
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. ");
}
}
Language:
Dutta
23 Jul, 2022 11:41 AM
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. ");
}
}
Language:
Dutta
23 Jul, 2022 11:41 AM
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. ");
}
}
Language:
Dutta
23 Jul, 2022 11:41 AM
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. ");
}
}
Language:
Dutta
23 Jul, 2022 11:41 AM
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. ");
}
}