I need to determine the current year in Java as an integer. I could just use java.util.Date()
, but it is deprecated.

- 27,862
- 20
- 113
- 121

- 14,485
- 12
- 47
- 58
-
4No, `java.util.Date` is *not* deprecated, not as of Java 8. Some of its methods are deprecated but not the entire class. But you should avoid both `java.util.Date` and `java.util.Calendar` as they are notoriously troublesome, confusing, and flawed. Instead use either [java.time](http://docs.oracle.com/javase/8/docs/api/java/time/package-summary.html) (in Java 8) or [Joda-Time](http://www.joda.org/joda-time/). – Basil Bourque May 03 '15 at 21:46
16 Answers
For Java 8 onwards:
int year = Year.now().getValue();
For older version of Java:
int year = Calendar.getInstance().get(Calendar.YEAR);

- 30,012
- 11
- 69
- 93
-
2Upon seeing your answer, Calendar would be a fine object to use. I was looking for a one-liner and I didn't think Calendar would have that for me. Proven wrong I am! Thanks! – karlgrz Sep 25 '08 at 22:03
-
1What about concurrency, what if other thread/piece of library code changes current time? Wouldn't it be reasonable to modify it to something like: Calendar c = Calendar.getInstance(); synchronized (c) {c.setTimeInMillis(System.currentTimeMillis()); return c.get(Calendar.YEAR);} – jnr Dec 05 '12 at 10:19
-
1This returns a different value in different versions of Android. I am comparing Android v4.1.2 with v4.2.1 . – Etienne Lawlor Jan 06 '13 at 08:06
-
I removed my SIM card from the phone with 4.1.2. So the network was not able to update the Date and Time on the phone. Consequently the phone was stuck in 2012. Looks like this is not a bug in 4.1.2 . – Etienne Lawlor Jan 09 '13 at 15:08
-
6@ycnix There's no concurrency issue with the code, so there is no need to make it more compilcated. `Calendar.getInstance().get(Calendar.YEAR)` is atomic in regards to the clock being changed externally, and doing `synchronized(c)` on a newly instance of a local variable would anyhow be very pointless here. – nos May 13 '14 at 12:46
-
8
-
FYI, the troublesome old date-time classes used here are now legacy, supplanted by the vastly improved java.time classes. – Basil Bourque Oct 23 '17 at 14:37
-
1It may be interesting to add the new way of doing it (detailed in the answer below) in Java 8. – Turtle Mar 21 '18 at 15:27
-
1
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();

- 321,522
- 82
- 660
- 783
-
10I suggest always passing the optional `ZoneId` argument to `now` rather than rely implicitly on the JVM’s current default zone (that can change at any moment during runtime!). – Basil Bourque May 23 '17 at 17:51
-
5Which looks likes this BTW: `int year = Year.now(ZoneId.of("America/New_York")).getValue();` https://docs.oracle.com/javase/8/docs/api/java/time/Year.html#now-- – bcmoney May 21 '20 at 07:34
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

- 49,809
- 17
- 109
- 135
-
This returns a different value in different versions of Android. I am comparing Android v4.1.2 with v4.2.1 . – Etienne Lawlor Jan 06 '13 at 10:09
-
1
-
Hey @toolkit, I have been doing some research with no luck. I was hoping for a more specific answer. – Etienne Lawlor Jan 07 '13 at 17:20
-
I removed my SIM card from the phone with 4.1.2. So the network was not able to update the Date and Time on the phone. Consequently the phone was stuck in 2012. Looks like this is not a bug in 4.1.2 . – Etienne Lawlor Jan 09 '13 at 22:22
You can also use 2 methods from java.time.YearMonth
( Since Java 8 ):
import java.time.YearMonth;
...
int year = YearMonth.now().getYear();
int month = YearMonth.now().getMonthValue();

- 55,661
- 15
- 90
- 140

- 712
- 1
- 8
- 14
-
1You incorrectly duplicate [another Answer](https://stackoverflow.com/a/33700141/642706) posted long ago. And you produce a month which is irrelevant to the Question. And you ignore the crucial issue of time zone. And your formatting is messy. I'm glad to see you participating in Stack Overflow. But this Answer is not good enough. Edit to correct its weaknesses and add more value beyond that already provided by the other Answers. Or delete your Answer before it collects down-votes. P.S. Super avatar icon! – Basil Bourque May 23 '17 at 17:56
-
Thanks a lot man. I've edited. link is a little bit different that my way – Zhurov Konstantin May 24 '17 at 07:07
tl;dr
ZonedDateTime.now( ZoneId.of( "Africa/Casablanca" ) )
.getYear()
Time Zone
The answer by Raffi Khatchadourian wisely shows how to use the new java.time package in Java 8. But that answer fails to address the critical issue of time zone in determining a date.
int year = LocalDate.now().getYear();
That code depends on the JVM's current default time zone. The default zone is used in determining what today’s date is. Remember, for example, that in the moment after midnight in Paris the date in Montréal is still 'yesterday'.
So your results may vary by what machine it runs on, a user/admin changing the host OS time zone, or any Java code at any moment changing the JVM's current default. Better to specify the time zone.
By the way, always use proper time zone names as defined by the IANA. Never use the 3-4 letter codes that are neither standardized nor unique.
java.time
Example in java.time of Java 8.
int year = ZonedDateTime.now( ZoneId.of( "Africa/Casablanca" ) ).getYear() ;
Joda-Time
Some idea as above, but using the Joda-Time 2.7 library.
int year = DateTime.now( DateTimeZone.forID( "Africa/Casablanca" ) ).getYear() ;
Incrementing/Decrementing Year
If your goal is to jump a year at a time, no need to extract the year number. Both Joda-Time and java.time have methods for adding/subtracting a year at a time. And those methods are smart, handling Daylight Saving Time and other anomalies.
Example in java.time.
ZonedDateTime zdt =
ZonedDateTime
.now( ZoneId.of( "Africa/Casablanca" ) )
.minusYears( 1 )
;
Example in Joda-Time 2.7.
DateTime oneYearAgo = DateTime.now( DateTimeZone.forID( "Africa/Casablanca" ) ).minusYears( 1 ) ;

- 303,325
- 100
- 852
- 1,154
-
2All zone IDs for the lazy ones (as me): https://www.mkyong.com/java8/java-display-all-zoneid-and-its-utc-offset/ – FBB Feb 07 '18 at 13:45
-
2@FBB I suggest the [Wikipedia page for the official IANA list](https://en.wikipedia.org/wiki/List_of_tz_database_time_zones) of time zone names. **Beware: These zones change surprisingly often.** If you really want up-to-date info, download a fresh copy of [the `tzdata` data file](https://www.iana.org/time-zones) yourself. You can peruse the file. And you can replace an outdated tzdata in your Java implementation. Oracle provides an [updater tool](http://www.oracle.com/technetwork/java/javase/tzupdater-readme-136440.html) for their JVM. – Basil Bourque Jul 02 '18 at 00:29
The easiest way is to get the year from Calendar.
// year is stored as a static member
int year = Calendar.getInstance().get(Calendar.YEAR);

- 7,038
- 6
- 33
- 44
-
-
-
2The get() API on Calendar gets the datum that is at the field specified by the constant. The year is located in the 1's field in the Calendar object! Calendar.getInstance() is getting the current date. http://java.sun.com/j2se/1.4.2/docs/api/java/util/Calendar.html#get(int) – Bob King Sep 25 '08 at 22:08
-
This returns a different value in different versions of Android. I am comparing Android v4.1.2 with v4.2.1 . – Etienne Lawlor Jan 06 '13 at 10:09
-
I removed my SIM card from the phone with 4.1.2. So the network was not able to update the Date and Time on the phone. Consequently the phone was stuck in 2012. Looks like this is not a bug in 4.1.2 . – Etienne Lawlor Jan 09 '13 at 22:21
If you want the year of any date object, I used the following method:
public static int getYearFromDate(Date date) {
int result = -1;
if (date != null) {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
result = cal.get(Calendar.YEAR);
}
return result;
}

- 4,129
- 9
- 51
- 89
Use the following code for java 8 :
LocalDate localDate = LocalDate.now();
int year = localDate.getYear();
int month = localDate.getMonthValue();
int date = localDate.getDayOfMonth();

- 12,987
- 11
- 98
- 148
You can also use Java 8's LocalDate
:
import java.time.LocalDate;
//...
int year = LocalDate.now().getYear();

- 4,701
- 8
- 43
- 62

- 3,042
- 3
- 31
- 37
-
1@AndrewT.,get [the ThreeTen Backport](http://www.threeten.org/threetenbp/) and it will work in Java 6 and 7 too. – Ole V.V. Oct 24 '17 at 02:16
If your application is making heavy use of Date and Calendar objects, you really should use Joda Time, because java.util.Date
is mutable. java.util.Calendar
has performance problems when its fields get updated, and is clunky for datetime arithmetic.

- 7,066
- 5
- 30
- 38
As some people answered above:
If you want to use the variable later, better use:
int year;
year = Calendar.getInstance().get(Calendar.YEAR);
If you need the year for just a condition you better use:
Calendar.getInstance().get(Calendar.YEAR)
For example using it in a do while that checks introduced year is not less than the current year-200 or more than the current year (Could be birth year):
import java.util.Calendar;
import java.util.Scanner;
public static void main (String[] args){
Scanner scannernumber = new Scanner(System.in);
int year;
/*Checks that the year is not higher than the current year, and not less than the current year - 200 years.*/
do{
System.out.print("Year (Between "+((Calendar.getInstance().get(Calendar.YEAR))-200)+" and "+Calendar.getInstance().get(Calendar.YEAR)+") : ");
year = scannernumber.nextInt();
}while(year < ((Calendar.getInstance().get(Calendar.YEAR))-200) || year > Calendar.getInstance().get(Calendar.YEAR));
}

- 329
- 2
- 10
You can do the whole thing using Integer math without needing to instantiate a calendar:
return (System.currentTimeMillis()/1000/3600/24/365.25 +1970);
May be off for an hour or two at new year but I don't get the impression that is an issue?

- 5,376
- 9
- 31
- 31
-
11are you working for apple, perhaps? http://www.macworld.com/article/2023580/ios-do-not-disturb-feature-oversleeps-on-new-year-s-day.html – Stefan Paul Noack Sep 08 '14 at 01:27
-
wow, this is very heavy, I would suggest to keep it simple like cagcowboy has shown it. – LStrike Jan 15 '16 at 10:08
-
1This may not be the best in all circumstances, but it doesn't deserve downvotes. – JohnnyLambada Oct 18 '18 at 18:17
In my case none of the above is worked. So After trying lot of solutions i found below one and it worked for me
import java.util.Scanner;
import java.util.Date;
public class Practice
{
public static void main(String[] args)
{
Date d=new Date();
int year=d.getYear();
int currentYear=year+1900;
System.out.println(currentYear);
}
}

- 985
- 2
- 12
- 32
-
That method is deprecated. And that terrible class was supplanted years ago by the *java.time* classes defined in JSR 310. See modern solution in the [Answer by assylias](https://stackoverflow.com/a/33700141/642706). – Basil Bourque Jun 11 '20 at 16:53
-
yeah but i was actively seeking for solution and tried many available solution and for me this one worked so i posted :) – Mayur Satav Jun 20 '20 at 10:04
I may add that a simple way to get the current year as an integer is importing java.time.LocalDate and, then:
import java.time.LocalDate;
int yourVariable = LocalDate.now().getYear()
Hope this helps!

- 11
- 2
-
Not bad. It’s already in a couple of the other answers, which is the only reason why I am not upvoting. – Ole V.V. Sep 07 '22 at 16:06
In Java version 8+ can (advised to) use java.time library. ISO 8601 sets standard way to write dates: YYYY-MM-DD
and java.time.Instant
uses it, so (for UTC
):
import java.time.Instant;
int myYear = Integer.parseInt(Instant.now().toString().substring(0,4));
P.S. just in case (and shorted for getting String
, not int
), using Calendar
looks better and can be made zone-aware.

- 3,423
- 7
- 36
- 71
-
1You are ignoring the crucial issue of time zone. See simpler solution in [other Answer](https://stackoverflow.com/a/33700141/642706). – Basil Bourque Jun 11 '20 at 16:56
I use special functions in my library to work 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 ) {
...
}

- 5,454
- 7
- 28
- 30