long time = -1;
        while (true) {
            Thread.sleep(1000);
            if (time == -1)
                time = System.currentTimeMillis();
            else
                time += 1000;
            System.out.println("Curernt time is:" + format.format(new Date(time)));
        }
不是高手, 试试而已. 可以吗?

解决方案 »

  1.   

    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    忘了这句
      

  2.   

    Timer好像和系统时间没有联系吧……
      

  3.   

    看来只能用线程来实现真正意义上的Timer了,
    哎,Timer类居然无法用,非要逼着用线程,
    可悲啊.....
      

  4.   

    import java.util.Calendar;
    import java.util.Timer;
    import java.util.TimerTask;public class TimerDemo {    public static void main(String[] args) {
            Timer timer = null;
            try {
                timer = new Timer(true);
                timer.schedule(new TestTask(), 0, 1000);
                while (true);
            } finally {
                timer.cancel();
            }
        }
    }class TestTask extends TimerTask {
        public void run() {
            System.out.println(Calendar.getInstance().getTime().toString());
        }
    }愿意的话,自己还可以添加结束循环的条件
      

  5.   

    import java.util.Timer;
    import java.util.TimerTask;/**
     * Simple demo that uses java.util.Timer to schedule a task to execute
     * once 5 seconds have passed.
     */public class Reminder {
        Timer timer;    public Reminder(int seconds) {
            timer = new Timer();
            timer.schedule(new RemindTask(), seconds*1000);
        }    class RemindTask extends TimerTask {
            public void run() {
                System.out.println("Time's up!");
                timer.cancel(); //Terminate the timer thread
            }
        }    public static void main(String args[]) {
            System.out.println("About to schedule task.");
            new Reminder(5);
            System.out.println("Task scheduled.");
        }
    }
      

  6.   

    Here抯 an example of using a timer to perform a task once per second. 
    public class AnnoyingBeep {
        Toolkit toolkit;
        Timer timer;    public AnnoyingBeep() {
    toolkit = Toolkit.getDefaultToolkit();
            timer = new Timer();
            timer.schedule(new RemindTask(),
                   0,        //initial delay
                   1*1000);  //subsequent rate
        }    class RemindTask extends TimerTask {
    int numWarningBeeps = 3;        public void run() {
        if (numWarningBeeps > 0) {
            toolkit.beep();
    System.out.println("Beep!");
    numWarningBeeps--;
        } else {
            toolkit.beep(); 
                    System.out.println("Time's up!");
            //timer.cancel(); //Not necessary because we call System.exit
            System.exit(0);   //Stops the AWT thread (and everything else)
        }
            }
        }
        ...
    }
      

  7.   

    //下面是一个每秒调用一次任务的例子 
    public class AnnoyingBeep {
        Toolkit toolkit;
        Timer timer;    public AnnoyingBeep() {
    toolkit = Toolkit.getDefaultToolkit();
            timer = new Timer();
            timer.schedule(new RemindTask(),
                   0,        //initial delay
                   1*1000);  //subsequent rate
        }    class RemindTask extends TimerTask {
    int numWarningBeeps = 3;        public void run() {
        if (numWarningBeeps > 0) {
            toolkit.beep();
    System.out.println("Beep!");
    numWarningBeeps--;
        } else {
            toolkit.beep(); 
                    System.out.println("Time's up!");
            //timer.cancel(); //Not necessary because we call System.exit
            System.exit(0);   //Stops the AWT thread (and everything else)
        }
            }
        }
    }
      

  8.   

    Timer和系统时间有什么关系?PS:感觉楼主是来挑衅的,不像问问题。
    看看楼主的名字 JuckFava (hehe)
      

  9.   

    5933811 欢迎大家一起加入JAVA群,有问题就可以在线讨论,及时回答!
      

  10.   

    Timer类是可以用的, 刚才就先用Timer类写了一个, 按照lzlzlzlz的理论完全可以实现. 只不过这么简单的需求需要用线程吗?
    你又没有说每隔一秒钟要处理一些事务.只是打印而已, 写个死循环就可以了. 任何语言都很简单吧.
      

  11.   

    介绍给你一个开源的东西吧,很好用,而且稳定,你要的需求都有http://www.quartzscheduler.org/ns/quartz