4

I'm creating a small game, and I'd like to set up a clock that runs off of real time. I'd also like it to be in 24 hour time, just for aesthetics. So, for example, if it was 3:45 PM here, how would I get the program to print "15:45" on the user's display?

How would I go about doing this, and what things would I use to begin this process?

Thank you very much.

If you were wondering, this was what I originally had, but I don't think it would really run unless the method was referenced, which causes the time to be correct only once throughout the day.

private static void clock (int time)
{
    int clock = time;

    boolean tick = true;
    while (tick)
    {
        clock ++;
        try {
             Thread.sleep(60000);                 //1000 milliseconds is one second.
        } catch(InterruptedException ex) {
             Thread.currentThread().interrupt();
        }

        if (clock > 2400)
        {
            clock = 0;
        }

    }
    System.out.println();
    System.out.println("The current time is " + clock + ".");
    System.out.println();
}

3 Answers3

2

pick System Date As :

public static String timeStamp = new SimpleDateFormat("dd_MMM_yyyy")
            .format(Calendar.getInstance().getTime());
1

A couple things....first, you should really familiarize yourself with the Java Date API, along with the DateFormat and SimpleDateFormat classes. Although it's not perfect, it can certainly do the job for you.
Not sure where that int is coming from but dates are generally based on the number of milliseconds since exactly midnight on January 1, 1970.
Second, you're close, at least in the core approach. After every minute, the thread wakes up, as you have it now, gets the current time in milliseconds, formats it as a String in the form of "HH:mm", and prints it out. At the end of the day (no pun intended), you're trying to do the opposite of what this person wanted. He/she had 24hr time but wanted 12hr time.

long timeInMillis = System.currentTimeMillis();
Calendar cal1 = Calendar.getInstance();
cal1.setTimeInMillis(timeInMillis);
SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm");
dateforrow = dateFormat.format(cal1.getTime());

Or something similar.

Community
  • 1
  • 1
MolonLabe
  • 168
  • 2
  • 8
  • This makes a lot of sense now. I'll try to memorize this information, and I appreciate it a lot. Thanks! –  Mar 29 '16 at 13:40
0

Run this Code in A constructor Of The Class Your Time Is shown Dynamic On Any Frame Or you can set to consloe also if you replace jTextField2.setText(time); to System.out.println(); method

    {
        final DateFormat timeFormat = new SimpleDateFormat("HH:mm:ss");
ActionListener timerListener = new ActionListener()
    {
        public void actionPerformed(ActionEvent e)
        {
            Date date = new Date();
            String time = timeFormat.format(date);
            jTextField2.setText(time);
        }
    };
Timer timer = new Timer(1000, timerListener);
    timer.setInitialDelay(0);
    timer.start(); 
        String date=new SimpleDateFormat("dd:MM:yyyy").format(Calendar.getInstance().getTime());
        jTextField3.setText(""+date);
}
Pritpal Singh
  • 163
  • 13