Java 控制台窗口上的数字时钟

Java 控制台窗口上的数字时钟,java,time,clock,Java,Time,Clock,您好,我的时钟代码只显示即时时间,但我如何才能使其数字化,这将在控制台中改变秒、分钟和小时 我需要的程序只是控制台…不与小程序 我的代码在这里: import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; public class DateFormatExample { public static void main(S

您好,我的时钟代码只显示即时时间,但我如何才能使其数字化,这将在控制台中改变秒、分钟和小时

我需要的程序只是控制台…不与小程序

我的代码在这里:

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);
    }
}
您可以这样尝试:

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();
    }
}
   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();

您可以尝试以下方法:

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();
    }
}
   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();

简单的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();
            }
        }
    }

将计时器(如
java.util.Timer
对象)与
TimerTask
对象一起使用。API和教程应该可以帮助您做到这一点。你能把我的代码转换成数字钟吗?
“你能把我的代码转换成数字钟吗?”
--不行。请不要让我们为你写代码。请检查链接,并尝试自己这样做。我们不是来帮你做家庭作业或作业的,那应该行。但是,请不要教年轻人使用早已过时且臭名昭著的
SimpleDateFormat
class。至少不是第一个选择。而且不是毫无保留的。今天,我们在及其
DateTimeFormatter
中有了更好的功能。