-1

嗨,我的时钟代码只是显示即时时间,但我怎样才能让它数字化,这将在控制台中更改秒、分和小时。

我只需要用于控制台的程序......而不是 Applet。

我的代码在这里:

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateFormatExample {
    public static void main(String[] args) {
        Date date = Calendar.getInstance().getTime();

        DateFormat formatter = new SimpleDateFormat("EEEE, dd MMMM yyyy, hh:mm:ss.SSS a");
        String today = formatter.format(date);      
        System.out.println("Today : " + today);
    }
}
4

3 回答 3

0

你可以这样尝试:

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateFormatExample {
    public static void main(String[] args) {
        Thread th = new Thread(new Runnable()
        {
            public void run()
            {
                while(true)
                {
                    Date date = Calendar.getInstance().getTime();
                    DateFormat formatter = new SimpleDateFormat("EEEE, dd MMMM yyyy, hh:mm:ss.SSS a");
                    String today = formatter.format(date);      
                    System.out.print("Today : " + today+"\r");
                    try{
                    Thread.sleep(100);}catch(Exception ex){}
                }
            }
        });
        th.start();
    }
}
于 2013-03-22T21:13:38.517 回答
0

你可以尝试这样的事情:

   new Thread(new Runnable() {

        @Override
        public void run() {

            while (true){

                Calendar cal = new GregorianCalendar();
                int hour = cal.get(Calendar.HOUR);
                int minute = cal.get(Calendar.MINUTE);
                int seconds = cal.get(Calendar.SECOND);
                String realTime = Integer.toString(hour) + " : " + Integer.toString(minute) + " : " + Integer.toString(seconds);

                System.out.println(realTime);

                try {

                    Thread.sleep(1000);
                } catch (Exception ex) {
                    ex.getStackTrace;
                }
           }
        }
    }).start();
于 2013-03-22T21:19:22.837 回答
0

简单的while循环就足够了:

public static void main(String[] args) {

        while (true) {
            Date date = Calendar.getInstance().getTime();
            DateFormat formatter = new SimpleDateFormat("EEEE, dd MMMM yyyy, hh:mm:ss.SSS a");
            String today = formatter.format(date);
            System.out.print("\rToday : " + today);
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
于 2018-08-21T10:16:07.073 回答