Add Or Subtract Days Weeks Months Years On Java 8 Date

Java 8 Date

In this tutorial I am going to show you how to add days, weeks, months, years to a particular date in Java 8 or later. Similarly I will also show how to substract days, weeks, months, years from a particular date.

The date is actually LocalDate API in Java 8 or later. The one of the important improvements in Java 8 date time API was introduced is thread safety. So date time API in Java 8 or later is thread safe and can be used in multi-threaded environment without applying synchronization.

I am also going to show you how to calculate days, weeks, months, years between two dates. I will also show how to start with a particular day of the month and year.

Prerequisites

Java 1.8+

Working with LocalDate

Now I will see how to get current date using Java 8 or later and I will do various operations on it.

The below code snippets will give you the current date:

LocalDate localDate = LocalDate.now();

System.out.println("Date: " + localDate);

The output will be Date: 2022-08-09.

Let’s say you want to add 5 days to this date, then you can do as given below:

localDate = localDate.plusDays(5);
System.out.println("Date: " + localDate);

The above code snippets will give you Date: 2022-08-14.

Let’s say I want to delay by 3 weeks, so I will add 3 weeks to the above date:

localDate = localDate.plusWeeks(3);
System.out.println("Date: " + localDate);

The above code snippets will give you below output: Date: 2022-09-04.

Now let’s say I want to display by 2 months, so I will add 2 months to the above date:

localDate = localDate.plusMonths(2);
System.out.println("Date: " + localDate);

The above code snippets will give you output as Date: 2022-11-04.

Let’s say I want to delay by one year, so I need to add 1 year to the above date:

localDate = localDate.plusYears(1);
System.out.println("Date: " + localDate);

Now if I want to get the original date back then I will subtract in the reverse order.

So first let’s subtract year from the above date:

localDate = localDate.minusYears(1);
System.out.println("Date: " + localDate);

It gives you Date: 2022-11-04.

Now subtract 2 months from the above date:

localDate = localDate.minusMonths(2);
System.out.println("Date: " + localDate);

This gives you Date: 2022-09-04.

I need to subtract 3 weeks from the above date:

localDate = localDate.minusWeeks(3);
System.out.println("Date: " + localDate);

The above code will give you output: Date: 2022-08-14.

Finally I need to subtract 5 days to get the original date:

localDate = localDate.minusDays(5);
System.out.println("Date: " + localDate);

This gives you Date: 2022-08-09.

Let’s say I want to start day with 10th of this month instead of the current day. Then I can do the following:

localDate = localDate.withDayOfMonth(10);
System.out.println("Date: " + localDate);

So your day starts with 10th, Date: 2022-08-10, instead of current day.

You can also start with a particular day of the year (total 365 or 366 days). The below code starts at 10th day of the year. So it becomes 10th January.

localDate = localDate.withDayOfYear(10);
System.out.println("Date: " + localDate);

This will give you output Date: 2022-01-10.

You can also change the month of the year or the year itself:

localDate = localDate.withMonth(9);
System.out.println("Date: " + localDate);

localDate = localDate.withYear(2025);
System.out.println("Date: " + localDate);

This gives you the following output:

Date: 2022-09-10
Date: 2025-09-10

Let’s say I want to calculate differences of days, weeks, months and years between two dates.

So here is the following code that calculates days, weeks, months, years differences between current date and another date.

DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd");
LocalDate anotherDate = LocalDate.parse("2020-12-23", dtf);

System.out.println("Another date: " + anotherDate);

System.out.println("Difference in days: " + ChronoUnit.DAYS.between(localDate, anotherDate));
System.out.println("Difference in weeks: " + ChronoUnit.WEEKS.between(localDate, anotherDate));
System.out.println("Difference in months: " + ChronoUnit.MONTHS.between(localDate, anotherDate));
System.out.println("Difference in years: " + ChronoUnit.YEARS.between(localDate, anotherDate));

The above code gives you the following output:

Current Date: 2025-09-10
Another date: 2020-12-23
Difference in days: -1722
Difference in weeks: -246
Difference in months: -56
Difference in years: -4

That’s all about manipulation on dates.

Source Code

Download

Leave a Reply

Your email address will not be published. Required fields are marked *