---
title: Add two dates
url: https://docs.vultr.com/java/examples/add-two-dates
publish_date: 2024-11-25T22:20:00.076551Z
last_updated: 2024-11-25T22:20:00.076551Z
header_image: https://docs.vultr.com/public/doc-assets/collection-items/1055/f0d8ac06-a6fb-4c48-b947-9c55004331b4.jpg
---

> Index of every Vultr Docs page in markdown: https://docs.vultr.com/llms.txt

# Java Program to Add Two Dates

## Introduction
Adding two dates in Java is a common requirement in many programming and application development scenarios, especially in areas like finance, scheduling, and logistics applications where date manipulation is frequently required. This task can be achieved through a variety of Java libraries and classes designed to handle date and time operations effectively.

In this article, you will learn how to add two dates in Java using different examples. Explore various methods to perform this operation effectively, applying both legacy classes like `java.util.Date` and newer classes from the Java 8 Date-Time API like `java.time.LocalDate`.

## Example: Using java.util.Date and java.util.Calendar

### Add Days to a Date
1. Import the necessary classes.
1. Create an instance of `java.util.Date` to represent the starting date.
1. Utilize `java.util.Calendar` to add a specified number of days to the date.

    ```java
    import java.util.Calendar;
    import java.util.Date;

    public class DateAdder {
        public static void main(String[] args) {
            Date currentDate = new Date(); // Current date
            int daysToAdd = 10; // Days to add

            Calendar calendar = Calendar.getInstance();
            calendar.setTime(currentDate);
            calendar.add(Calendar.DAY_OF_MONTH, daysToAdd);

            Date newDate = calendar.getTime();
            System.out.println("New Date: " + newDate);
        }
    }
    ```

    Here, `Calendar.getInstance()` obtains a calendar using the default time zone and locale. The calendar is then set to `currentDate`, and days are added using `Calendar.add()`. The `newDate` will have the additional days included.

### Adding Months and Years
1. Follow similar steps to add different time units like months and years using `Calendar`.

    ```java
    calendar.add(Calendar.MONTH, 5);
    calendar.add(Calendar.YEAR, 2);
    ```

    These lines add 5 months and 2 years to the `currentDate`, respectively.

## Example: Using Java 8 Date-Time API

### Using LocalDate to Add Days, Months, or Years
1. Import `java.time.LocalDate`.
1. Initialize a `LocalDate` object.
1. Use `plusDays`, `plusMonths`, or `plusYears` methods to add time units.

    ```java
    import java.time.LocalDate;

    public class DateAdderJava8 {
        public static void main(String[] args) {
            LocalDate currentDate = LocalDate.now(); // Current date
            LocalDate newDate = currentDate.plusDays(10).plusMonths(2).plusYears(1);

            System.out.println("New Date: " + newDate);
        }
    }
    ```

    This approach is cleaner and more intuitive than using the older `Calendar` class. `LocalDate` represents a date without time-of-day or time-zone, which can vastly simplify day-to-day date handling.

## Conclusion
Adding two dates in Java can be handled efficiently using either classic approaches with `java.util.Calendar` or more modern methods provided by the Java 8 Date-Time API. The choice of method will largely depend on your specific requirements and the minimum Java version you support. Learn to harness these techniques to handle date manipulations adeptly within your Java applications, ensuring that you provide accurate calculations and robust functionalities.