JLink
JLink

Reputation: 307

Java Thread to find System Time

I used bellow thread to find system time. But there is a problem here. It always shows 12:11:-- PM and seconds run but after 60 seconds 12:11 no change in minutes again seconds run..My computer original time is 12:24 PM..what is the problem here.sorry for my bad English..

public void statTime(){
    new Thread(new Runnable() {

        @Override
        public void run() {
            try {
                while (true) {                            
                    java.util.Date d = new java.util.Date();
                    SimpleDateFormat sdf = new SimpleDateFormat("hh:MM:ss a");
                    jLabel1.setText(sdf.format(d));
                    Thread.sleep(1000);

                }

            } catch (Exception e) {
            }
        }
    }).start();
}

Upvotes: 0

Views: 3076

Answers (2)

Aniket Thakur
Aniket Thakur

Reputation: 68975

Better use System time using following code

SimpleDateFormat sdf = new SimpleDateFormat("hh:MM:ss a");
jLabel1.setText(sdf.format(new Date(System.currentTimeMillis())));

Upvotes: 0

DaveH
DaveH

Reputation: 7335

MM is month in year - try mm in your formatter string

Upvotes: 2

Related Questions