Get current year in Java

Get current year in java

In this post, we will see how to get current year in Java.

Using Date and SimpleDateFormat

The Date class was introduced in Java 1.0 and provided different approaches to work with time.

The SimpleDateFormat inherits from DateFormat and is mainly used to parse a date to text.

You can provide your custom pattern to the constructor of this SimpleDateFormat, and it will pass the desired date to text, but we will use the applyPattern() method in our case.

The default pattern that indicates a year is represented by the letter y, and to get the full year, just pass the string yyyy to the applyPattern() method.

This method applies the pattern to the given date but returns no value. If the pattern is null, the method throws a NullPointerException.

If the pattern given is not recognized, the method throws an IllegalArgumentException.

After applying the pattern, pass a date object to the format() method from SimpleDateFormat, which returns a string with the formatted date.



Using Calendar

The date and time fields should be converted using Calendar while formatting and parsing of date and time should be done using DateFormat.

The getInstance() method from the Calendar class returns a calendar using the default time zone and locale.

The calendar class is locale-sensitive, and if the locale contains a time zone with Unicode extension "tz", the time zone is used alternatively.

Finally, use the calendar get() method and pass Calendar.YEAR. The method returns the value of the calendar field, which represents the current year.


</div class="content-box-green">

Using LocalDate

From the two examples we have already covered, note that Date and Calendar comes from the util package and LocalDate come from the time package.

This package was introduced in Java 8 in place of Java util package API, which was not thread safe and had concurrency issues.

The time API is immutable meaning it returns a copy of the original object instead of returning the state of the original object, making it thread-safe.

LocalDate provides a date without a time zone, and the only way to get an instant on a timeline is by additional information such as offset and time zone.

After getting the current date using now() method, use the getYear() method of LocalDate, which returns the int value of the year from MIN_YEAR to MAX_YEAR.



Using LocalDateTime

The LocalDateTime works the same way as LocalDate but provides a time instant that is not present in LocalDate.

As with other time classes of Java 8, Avoid using reference equality, identity hashcode, or synchronization as they are identity-sensitive operations and may have unpredictable results.

After getting the current date and time using now(), use the getYear() method, which returns the int value for the current year.



using ZonedDateTime

This class works the same way as LocalDate with the addition of ZonedId and ZoneOffset, which are very crucial when dealing with data from different time zones.

A zoneId is indicated as Europe/Paris, and a ZoneOffset is a number with a plus or minus indicating how to adjust time with the UTC such as +03:00.

ZonedDateTime has no constructor and uses the static now() method to get the current date, time, offset, and time zone using the system clock and the default time zone.

After getting the current date instance, use the getYear() method, which returns an int value for the current year.



Using OffsetDateTime

When dealing with applications that communicate to a database or a network protocol, then use OffsetDateTime, which is suited for this purpose.

When dealing with applications that are not very detailed, use the ZonedDateTime and Instant classes.

As the class name suggests, this class is accompanied by an offset from the UTC/Greenwich in the ISO-8601 calendar system.

To get the current year, use the now() method from OffsetDateTime the call the getYear() method, which returns an int value for the current year.



Using Instant and ZoneId

Instant gives you the actual date and gets rid of the offset and time zone, and getting a date from the timeline requires additional information such as ZoneId.

The Instant class uses the now() method to query the system clock for the current Instant.

To get the current year, we have to return a ZonedDateTime by passing a ZoneId to the atZone() method from the Instant class.

Use the systemDefault() method from ZoneId to get the system’s default ZoneId which returns an id such as Europe/Paris.

From the ZonedDateTime, you can use the getYear() method, which returns the current year.



Using Instant and ZoneOffset

ZoneOffset is the amount of time that differs from the UTC/Greenwich, and we can use the offset with Instant to get the current year.

To get the current year, use atOffset() method from the Instant class and pass a ZoneOffset to it using ZoneOffset.of() which will return an OffsetDateTime.

Use the getYear() method from OffsetDateTime class which returns the current year as we had seen earlier.

The offset can have any combination of +hh:mm: ss or -hh:mm: ss, indicating hours, minutes, and seconds.




In this tutorial, you have learned how to get current year in java using the old date API using which covered Date and Calendar classes. The second part covered the new time API introduced in Java 8 and included using LocalDate, LocalDateTime, ZonedDateTime, OffsetDateTime, Instant and ZoneId, and lastly using Instant and ZoneOffset.


Related Posts

Leave a Reply

Your email address will not be published.

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.