Get integer value of the current year in Java Get integer value of the current year in Java java java

Get integer value of the current year in Java


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

Not sure if this meets with the criteria of not setting up a new Calendar? (Why the opposition to doing so?)


Using Java 8's time API (assuming you are happy to get the year in your system's default time zone), you could use the Year::now method:

int year = Year.now().getValue();


This simplest (using Calendar, sorry) is:

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

There is also the new Date and Time API JSR, as well as Joda Time