Javatpoint Logo
Javatpoint Logo

How to add 4 years to Date in Java?

In Java, we can add 4 years to the date using the Calendar or LocalDate class. In this section, we'll discuss both approaches and show how to implement them in Java code.

Using the Calendar Class

The Calendar class is a legacy class that was introduced in Java 1.1 and provides a set of methods for working with dates, times, and time zones. It has several fields to represent different aspects of a date, such as year, month, day, hour, minute, and second. The Calendar class also provides methods to add or subtract a certain amount of time from a date, set a specific date or time, and format a date into a string.

The Date class is another legacy class that was introduced in Java 1.0 and represents a specific instant in time with millisecond precision. It is similar to the Calendar class but is simpler and less powerful. The Date class provides methods to convert a date to a string, compare dates, and perform arithmetic operations on dates.

The Calendar class is a part of the Java.util package and provides several methods to manipulate dates and times. To add 4 years to the current date using the Calendar class, follow these steps:

1. Get the current date using the getInstance() method of the Calendar class.

2. Use the add() method to add 4 years to the current date.

3. Get the updated date using the getTime() method.

4. Convert the updated date to a specific date format using the SimpleDateFormat class.

Here's the complete Java code to add 4 years to the current date using the Calendar class:

Date.java

Output:

Updated Date: 2027-4-21

Using the LocalDate Class

The LocalDate class is a newer class that was introduced in Java 8 and provides a simpler and more intuitive way to work with dates. It represents a date without a time zone and provides methods to add or subtract a certain amount of time from a date, set a specific date, and format a date into a string. The LocalDate class also supports other useful methods, such as isBefore(), isAfter(), and compareTo(), to compare dates.

When adding a certain duration to a date, it is important to consider the specific requirements of the application. For example, if the application needs to work with time zones, it may be better to use the Calendar class or the DateTime class instead of the Date class. Similarly, if the application needs to work with dates only, it may be more appropriate to use the LocalDate class instead of the Calendar class or the Date class.

The LocalDate class is a part of the Java.time package introduced in Java 8. It provides a more user-friendly way to work with dates compared to the Calendar class. To add 4 years to the current date using the LocalDate class, follow these steps:

1. Get the current date using the now() method of the LocalDate class.

2. Use the plusYears() method to add 4 years to the current date.

3. Convert the updated date to a specific date format using the DateTimeFormatter class.

Here's the complete Java code to add 4 years to the current date using the LocalDate class:

Date.java

Output:

Updated Date: 2027-4-21

In this section, we have discussed two ways to add 4 years to date in Java. We can use either the Calendar class or the LocalDate class depending on our preference. The Calendar class is available in all versions of Java, while the LocalDate class is available in Java 8 and later. Both approaches are simple and easy to implement in your Java code.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA