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.
-
1
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.