Solved

A program to calculate the elapsed time

Posted on 1998-07-27
8
356 Views
Last Modified: 2010-03-30
I hope that somebody can help me  write a program to calculate the elapsed time
between the start time and the end time (both values are entered by the user in
TextField txtstartTime and TextField txtendTime.)
For example:  elapsed time = 13:30:01(endTime) - 10:45:10(startTime);
0
Comment
Question by:CG
  • 4
  • 4
8 Comments
 

Author Comment

by:CG
ID: 1228433
Adjusted points to 60
0
 
LVL 6

Expert Comment

by:gadio
ID: 1228434
    SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yy HH:mm");    
     SimpleDateFormat resformat = new SimpleDateFormat("HH:mm");

     Date sd = formatter.parse(txtstartField.getText());
     Date ed = formatter.parse(txtendField.getText());
     Date diff = ed.getTime()-sd.getTime();
     System.out.println( resformat.format(diff) );

I hope that works since I havn't compiled it...  :-)
G.
0
 
LVL 6

Expert Comment

by:gadio
ID: 1228435
You have to change the format definition slightly if you want to use seconds as well. Look at the SimpleDateFormat api. If you have problems, ask.
0
VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

 

Author Comment

by:CG
ID: 1228436
Hi, Gadio:
Thanks for your answer. According to your proposed answer, I wrote a program to
test it. But there was one compiler error: Identifier expected
            System.out.println (resformat.format(diff) );

import java.awt.*;
import java.awt.event.*;
import java.text.SimpleDateFormat;

public class time extends Frame
{
        static public void main(String args[])
        {      
      (new time()).setVisible(true);
        }
        public time()
        {      
                super( "Elapsed Time" );
                //{{INIT_CONTROLS
                setLayout(null);
        setSize(200,120);
            
        lbstartTime = new Label("Start Time:");
        lbstartTime.setBounds(20,60,80,25);
        add(lbstartTime);            
                txtstartTime = new TextField();
        txtstartTime.setBounds(110,60,72,25);
        add(txtstartTime);            
                lbendTime = new Label("End Time:");
        lbendTime.setBounds(20,70,80,25);
        add(lbendTime);      
                txtendTime = new TextField();
        txtendTime.setBounds(110,80,72,25);
        add(txtendTime);            

                //{{REGISTER_LISTENERS
            this.addWindowListener(new WindowAdapter(){
                  public void windowClosing(WindowEvent e){
                    System.exit(0);}
                });
      }      
       
        Label lbstartTime, lbendTime;
        TextField txtstartTime, txtendTime;
         
        SimpleDateFormat resformat = new SimpleDateFormat("hh:mm:ss");

        Date sd = resformat.parse(txtstartTime.getText());
        Date ed = resformat.parse(txtendTime.getText());
        Date diff = ed.getTime()-sd.getTime();
        System.out.println( resformat.format(diff) );
     
       
 }
0
 
LVL 6

Expert Comment

by:gadio
ID: 1228437
I fixed it up:

import java.awt.*;
import java.awt.event.*;
import java.text.SimpleDateFormat;
import java.util.Date;

public class time extends Frame
{
  Label lbstartTime, lbendTime;
  TextField txtstartTime, txtendTime;
  Button calculate;
  SimpleDateFormat formatter;
  SimpleDateFormat resformat;

  static public void main(String args[])
  {
        time t = new time();
        t.show();;
  }
  public time()
  {
        super( "Elapsed Time" );
        //{{INIT_CONTROLS
        setLayout(new FlowLayout());
        //setSize(200,120);

        lbstartTime = new Label("Start Time:");
        lbstartTime.setBounds(20,60,80,25);
        add(lbstartTime);
        txtstartTime = new TextField(10);
        txtstartTime.setBounds(110,60,72,25);
        add(txtstartTime);
        lbendTime = new Label("End Time:");
        lbendTime.setBounds(20,70,80,25);
        add(lbendTime);
        txtendTime = new TextField(10);
        txtendTime.setBounds(110,80,72,25);
        add(txtendTime);
        calculate = new Button("Calculate!");
        add(calculate);
        pack();

        formatter = new SimpleDateFormat("HH:mm:ss");
        resformat = new SimpleDateFormat("HH:mm:ss");
        //{{REGISTER_LISTENERS
        this.addWindowListener(new WindowAdapter(){
          public void windowClosing(WindowEvent e){
                System.exit(0);}
        });
        calculate.addActionListener(new ActionListener(){
          public void actionPerformed(ActionEvent e) {
                try {
                  Date sd = formatter.parse(txtstartTime.getText());
                  Date ed = formatter.parse(txtendTime.getText());
                  Date diff = new Date( ed.getTime()-sd.getTime()+ 28800000 );
                  System.out.println( resformat.format(diff) );
                } catch( Exception ex ) {
                  System.out.println( ex );
                }
          }
        });
  }

}


0
 

Author Comment

by:CG
ID: 1228438
gadio:
Thanks again for your help! I tested your code. Here is a problem:
When I entered Start Time 10:30:10, and End Time 12:50:10, I got the
result 05:20:00 (should be 02:20:00). The result should be set to TextField
elapsedTime. Could you please help me to fix it?
I'm looking forward to receiving your answer soon.
0
 
LVL 6

Accepted Solution

by:
gadio earned 60 total points
ID: 1228439
Oh. There are problems with the time zone I think. use the following: replace the last try block with:

                try {
                  Date sd = formatter.parse(txtstartTime.getText());
                  Date ed = formatter.parse(txtendTime.getText());
                  long diff = ed.getTime()-sd.getTime();
                  diff /=1000;
                  long seconds = diff % 60;
                  diff /= 60;
                  long min = diff % 60;
                  diff /= 60;
                  long hours = diff;
                  System.out.println( (hours<10?"0":"")+hours+":"+(min<10?"0":"")+min+":"+(seconds<10?"0":"")+seconds );
                } catch( Exception ex ) {
                  System.out.println( ex );
                }

0
 

Author Comment

by:CG
ID: 1228440
Thanks, gadio. You are terrific!
0

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How to monitor the number of connections/threads being utilized on Tomcat 6.0 8 70
servlet doXXX methods 3 49
MySQL  on Tomcat 8 45
null output 3 25
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question