How to Get a System Date in Java

How to Get a System Date in Java thumbnail
Get system time in Java.

Java developers who need to program applications that use a computer's system time use an internal function to retrieve the value. The system time is the calendar date shown on your task bar. If computers are synchronized with a server, the dates on the network machines are typically the same. However, retrieving the date ensures that the Java software is synchronized with the local machine, and the software shows the same time as the computer user's system clock.

Instructions

    • 1

      Create a string that contains the format for the time display. For instance, if you want to show the time as "hours and minutes," you must specify the format for Java. The following code sets up a format string for hours, minutes and seconds:

      String dateFormat = "HH:mm:ss";

    • 2

      Get an instance of the calendar object. This sets up an object from which you can get the time. The following code shows you how to call the calendar:

      Calendar objCal = Calendar.getInstance();

    • 3

      Format the time and display it to the user. The following code formats the calendar time and prints it to the user's console:

      SimpleDateFormat date = new SimpleDateFormat(dateFormat);
      System.out.println(date.format(objCalendar.getTime());

Related Searches:

References

  • Photo Credit clock image by Elena Nabokova from Fotolia.com

Comments

You May Also Like

Related Ads

Featured