Friday, October 16, 2009

Java : Time elapsed counter using swing

Java program that calculates time elapsed from start time, using this you can implement stop watch. Logic is simple just get start time (time when program started) and increment the time counter by current time minus start time ie (elapsedTime = currentTime - starTime).

Here is the complete tested code, directly you can run this code and see output.


/* TimeElapsed.java */

import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
import java.text.*;

public class TimeElapsed extends JFrame
{
JLabel time;

long startTime = System.currentTimeMillis();

TimeElapsed()
{
setSize(380,200);
setTitle("http://simpleandeasycodes.blogspot.com/");
setLocation(100,100);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

setLayout(new GridBagLayout());

time = new JLabel("");

time.setFont(new Font("SansSerif",Font.BOLD, 36));

time.setForeground(Color.MAGENTA);

add(time);

//starting new Thread which will update time
new Thread(new Runnable()
{
public void run()
{ try
{
updateTime();
}
catch (Exception ie)
{ }
}
}).start();
}

public void updateTime()
{
try
{
while(true)
{
//geting Time in desire format
time.setText(getTimeElapsed());
//Thread sleeping for 1 sec
Thread.currentThread().sleep(1000);
}
}
catch (Exception e)
{
System.out.println("Exception in Thread Sleep : "+e);
}
}

public String getTimeElapsed()
{
long elapsedTime = System.currentTimeMillis() - startTime;
elapsedTime = elapsedTime / 1000;

String seconds = Integer.toString((int)(elapsedTime % 60));
String minutes = Integer.toString((int)((elapsedTime % 3600) / 60));
String hours = Integer.toString((int)(elapsedTime / 3600));

if (seconds.length() < 2)
seconds = "0" + seconds;

if (minutes.length() < 2)
minutes = "0" + minutes;

if (hours.length() < 2)
hours = "0" + hours;

return hours+":"+minutes+":"+seconds;
}

public static void main(String[] args)
{
JFrame obj = new TimeElapsed();
obj.setVisible(true);
}
}



Output