How to Get the Current Year in Java

The "GregorianCalendar" class provides a series of methods retrieving different portions of the current time according to the customary calendar of the Western tradition. It is included as part of the Java standard library, and though dealing with dates in Java can be get complicated, simply retrieving the current year from the system clock is refreshingly easy.

Instructions

    • 1

      Open Netbeans or your favorite Java Integrated Development Environment (IDE).

    • 2

      Create a new class named "YearTest" by clicking "File" and "New class."

    • 3

      Type "psvm." This will be expended to a valid "main" method.

    • 4

      Type the following into the main method:

      int year = GregorianCalendar().get(Calendar.YEAR);

      This creates a new calendar using the current system time and retrieves the "YEAR" portion of the date from it as an integer.

Tips & Warnings

  • Java determines the current year by checking the system time on the computer where it is running. If the system time is wrong, then Java will return the wrong values.

Related Searches:

References

Comments

You May Also Like

  • How to Get a System Date 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...

  • How to Create a Calendar in Java

    Java's calendar class stores information about a moment in time. The class can be used to compare with different times, to manipulate...

Related Ads

Featured