import java.util.*;
import javax.swing.*;
public class Clock {
public static void main(String[] args) {
JFrame jf = new JFrame("Clock");
JLabel jl = new JLabel("Clock");
jl.setHorizontalAlignment(JLabel.CENTER);
jf.add(jl,"Center");
jf.setBounds(500,300,200,100);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setVisible(true);
Thread t = new MyThread(jl);
t.start();
}
}
class MyThread extends Thread{
private JLabel jl;
public MyThread(JLabel jl){
this.jl = jl;
}
public void run(){
while(true){
jl.setText(this.getTime());
try{
Thread.sleep(1000);
}catch(InterruptedException e){
e.printStackTrace();
}
}
}
private String getTime() {
Calendar c =new GregorianCalendar();
String time = c.get(Calendar.YEAR)+"-"+(c.get(Calendar.MONTH)+1)+
"-"+c.get(Calendar.DATE)+" ";
int h = c.get(Calendar.HOUR_OF_DAY);
int m = c.get(Calendar.MINUTE);
int s = c.get(Calendar.SECOND);
String sh = h < 10 ? "0" : "";
String sm = m < 10 ? "0" : "";
String ss = s < 10 ? "0" : "";
time += sh + h + ":" + sm + m + ":" + ss + s;
return time;
}
}