Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the current time and set it to a clock? [duplicate]

Tags:

java

datetime

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

1 Answers

pick System Date As :

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