Displaying Time

Hi I have a form in which I need to display the time updated each second.I'm using Netbeans for my project.How can I do that?
[140 byte] By [renjua] at [2007-11-27 3:47:12]
# 1
SimpleDateFormat for the display formatJLabel to display the timejavax.swing.Timer (set for 1 sec) to set the label's time
Michael_Dunna at 2007-7-12 8:50:58 > top of Java-index,Java Essentials,New To Java...
# 2
This worked for me..long finTime=0;long startTime=0;while(true){endTime = System.currentTimeMillis();while (finTime < 1) { endTime = System.currentTimeMillis(); finTime = (endTime - startTime) / 1000;}}
sridanua at 2007-7-12 8:50:58 > top of Java-index,Java Essentials,New To Java...
# 3
@MichaelCan you explain please. I couldn't understand
renjua at 2007-7-12 8:50:58 > top of Java-index,Java Essentials,New To Java...
# 4

This worked for me..

long finTime=0;

long startTime=0;

while(true)

{

endTime = System.currentTimeMillis();

while (finTime < 1)

{

endTime = System.currentTimeMillis();

finTime = (endTime - startTime) / 1000;

}

}

sorry ... pressed the post button twise.

Message was edited by:

sridanu

sridanua at 2007-7-12 8:50:58 > top of Java-index,Java Essentials,New To Java...
# 5
I guess, this will work. But it will not show the time in it real format.I need it in hh:mm format. Do you have any other method?
renjua at 2007-7-12 8:50:58 > top of Java-index,Java Essentials,New To Java...
# 6

import java.util.*; //for manipulating the time

import java.text.*;

import org.apache.commons.lang.time.*; // formatting numbers

public static String returnDateAndTime(long currentTime)

{

return DateFormatUtils.format(currentTime, "dd-MMM-yyyy HH:mm:ss");

}

use it as System.out.println(returnDateAndTime(System.currentTimeMillis());

sridanua at 2007-7-12 8:50:58 > top of Java-index,Java Essentials,New To Java...
# 7

import java.text.SimpleDateFormat;

import java.util.Calendar;

import java.util.Date;

import java.util.GregorianCalendar;

public class Time

{

public Time()

{

}

public static void main(String args[])

{

Time t=new Time();

String time=t.CallTime();

System.out.println("Time: "+time);

}

public String CallTime()

{

Date date=new Date();

SimpleDateFormat sdate=new SimpleDateFormat("hh:mm:ssa");

GregorianCalendar cal = new GregorianCalendar();

cal.setTime(date);

int hour24 = cal.get(Calendar.HOUR_OF_DAY);

int minute = cal.get(Calendar.MINUTE);

String result=hour24 +":"+minute;

return result;

}

}

sony_tja at 2007-7-12 8:50:58 > top of Java-index,Java Essentials,New To Java...
# 8
Yes this also works.But my problem is to update time in each second which will be shown in a label.
renjua at 2007-7-12 8:50:58 > top of Java-index,Java Essentials,New To Java...
# 9
Like Michael said, use a timer to update the label.
nogoodatcodinga at 2007-7-12 8:50:58 > top of Java-index,Java Essentials,New To Java...
# 10
SimpleDateFormat for the display formatJLabel to display the timejavax.swing.Timer (set for 1 sec) to set the label's timeBut can you explain it in detail.
renjua at 2007-7-12 8:50:58 > top of Java-index,Java Essentials,New To Java...
# 11

> SimpleDateFormat for the display format

> JLabel to display the time

> javax.swing.Timer (set for 1 sec) to set the label's

> time

>

> But can you explain it in detail.

The [url=http://java.sun.com/j2se/1.5.0/docs/api/javax/swing/Timer.html]API documentation[/url] explains it (and provides a link to a tutorial as well.)

TimTheEnchantora at 2007-7-12 8:50:58 > top of Java-index,Java Essentials,New To Java...
# 12
Thanks a lot;It is working.
renjua at 2007-7-12 8:50:58 > top of Java-index,Java Essentials,New To Java...