How to calculate the interval between 2 dates

0 votes
How do we write the code which takes in two dates and time and calculates the interval between the dates and prints it in the following format:
The elapsed time is: ** Days ** Hours ** Minutes ** Seconds
**---> relevant data
Aug 29, 2018 in Java by curious
• 560 points
687 views

1 answer to this question.

0 votes

The following code might be helpful:

public static void main(String[] args)

      {

            Scanner reader = new Scanner(System.in); 

            System.out.println("Enter two dates and time in the following format (yy/MM/dd HH:mm:ss) : ");

            String dateStart = reader.nextLine();

            String dateStop = reader.nextLine();

            reader.close();

           SimpleDateFormat format = new SimpleDateFormat("yy/MM/dd HH:mm:ss");

           java.util.Date d1 = null;

           java.util.Date d2 = null;

           try

          {

                  d1 =  format.parse(dateStart);

           }

          catch (java.text.ParseException e)

          {

                  e.printStackTrace();

           }

           try

           {

                  d2 =  format.parse(dateStop);

           }

           catch (java.text.ParseException e)

           {

                  e.printStackTrace();

           }

           long d = d2.getTime() - d1.getTime();

           long dS = d / 1000 % 60;

           long dM = d / (60 * 1000) % 60;

           long dH = d / (60 * 60 * 1000);

           long days=0;

           if (dH>24)

           {

            days = dH/24;

            dH = dH - (24*days);

          }

          System.out.println("The elapsed time is: "+days+" Days "+dH+" Hours "+dM+ " Minutes "+dS+" Seconds ");       

      }

}

answered Aug 29, 2018 by bug_seeker
• 15,520 points

Related Questions In Java

0 votes
1 answer

How to calculate the difference between two date instances in Java?

You can use Joda Time Library. Interval i ...READ MORE

answered May 4, 2018 in Java by Parth
• 4,630 points
767 views
+1 vote
1 answer

How to calculate days between two dates?

You are making some conversions with your ...READ MORE

answered Aug 28, 2018 in Java by Frankie
• 9,830 points
2,605 views
0 votes
1 answer

How can I calculate number of days between two dates?

You may refer this. This might work ...READ MORE

answered Jul 19, 2018 in Java by Akrati
• 3,190 points
1,041 views
0 votes
2 answers

How to create a 2-D array in java?

int[][] multi = new int[5][]; multi[0] = new ...READ MORE

answered Jul 16, 2018 in Java by Daisy
• 8,120 points
972 views
0 votes
1 answer

How to print java array in the simplest way?

String[] arr = new String[] {"John", "Mary", ...READ MORE

answered Apr 17, 2018 in Java by sophia
• 1,400 points
649 views
0 votes
3 answers

Check if a String is numeric in Java

Java 8 Lambda Expression is used: String someString ...READ MORE

answered Sep 3, 2018 in Java by Daisy
• 8,120 points
3,379 views
0 votes
2 answers

Generate an alpha-numeric string randomly

Java supplies a way of doing this ...READ MORE

answered Jul 18, 2018 in Java by Daisy
• 8,120 points
2,234 views
0 votes
1 answer

How do I create a Java string from the contents of a file?

If you're looking for an alternative that ...READ MORE

answered Apr 19, 2018 in Java by Rishabh
• 3,620 points
949 views
0 votes
2 answers

Counting no of Occurrence of a particular character inside a string in Java

We can find out the no. of ...READ MORE

answered Sep 7, 2018 in Java by Sushmita
• 6,910 points
2,311 views
+12 votes
3 answers

How do I set or change the PATH system variable?

First make sure you have installed JDK ...READ MORE

answered Aug 28, 2018 in Java by slayer
• 29,350 points
979 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP