Solved

A program to calculate the elapsed time

Posted on 1998-07-27
349 Views
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
Question by:CG
• 4
• 4

Author Comment

0

LVL 6

Expert Comment

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

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

Author Comment

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);
txtstartTime = new TextField();
txtstartTime.setBounds(110,60,72,25);
lbendTime = new Label("End Time:");
lbendTime.setBounds(20,70,80,25);
txtendTime = new TextField();
txtendTime.setBounds(110,80,72,25);

//{{REGISTER_LISTENERS
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

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);
txtstartTime = new TextField(10);
txtstartTime.setBounds(110,60,72,25);
lbendTime = new Label("End Time:");
lbendTime.setBounds(20,70,80,25);
txtendTime = new TextField(10);
txtendTime.setBounds(110,80,72,25);
calculate = new Button("Calculate!");
pack();

formatter = new SimpleDateFormat("HH:mm:ss");
resformat = new SimpleDateFormat("HH:mm:ss");
//{{REGISTER_LISTENERS
public void windowClosing(WindowEvent e){
System.exit(0);}
});
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

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
0

LVL 6

Accepted Solution

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

0

Featured Post

Suggested Solutions

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.