# Get integer value of the current year in Java

I require to establish the existing year in Java as an integer. I can simply make use of java.util.Date(), yet it is deprecated.

350
2022-07-22 15:25:09
Source Share

You can do the entire point making use of Integer math without requiring to instantiate a calendar:

return (System.currentTimeMillis()/1000/3600/24/365.25 +1970);


May be off for a hr or 2 at new year yet I do not think that is a concern?

7
2022-07-24 03:12:17
Source

I make use of unique features in my collection to collaborate with days/month/year ints -

int[] int_dmy( long timestamp ) // remember month is [0..11] !!!
{
Calendar cal = new GregorianCalendar(); cal.setTimeInMillis( timestamp );
return new int[] {
cal.get( Calendar.DATE ), cal.get( Calendar.MONTH ), cal.get( Calendar.YEAR )
};
};

int[] int_dmy( Date d ) {
...
}

0
2022-07-24 03:03:33
Source
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?)

733
2022-07-22 17:03:11
Source

The easiest way is to get the year from Calendar.

// year is stored as a static member
int year = Calendar.getInstance().get(Calendar.YEAR);

15
2022-07-22 17:02:54
Source

This most basic (making use of Calendar, sorry) is:

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


There is additionally the new Date and Time API JSR, along with Joda Time

28
2022-07-22 17:02:43
Source

If your application is making hefty use Date and also Calendar things, you actually need to make use of Joda Time, due to the fact that java.util.Date is mutable. java.util.Calendar has performance troubles when its areas get upgraded, and also is confusing for datetime math.

6
2022-07-22 16:24:53
Source