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

At least Java 8

Working with LocalDate

Now we will see how to get current date using Java 8 or later and we 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: 2020-07-06.

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: 2020-07-11.

Let’s say we want to delay by 3 weeks, so we 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: 2020-08-01.

Now let’s say we want to display by 2 months, so we 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: 2020-10-01.

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

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

Now if we want to get the original date back then we 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: 2020-10-01.

Now subtract 2 months from the above date:

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

This gives you Date: 2020-08-01.

We 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: 2020-07-11.

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

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

This gives you Date: 2020-07-06.

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

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

So your day starts with 10th, Date: 2020-07-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: 2020-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: 2020-09-10
Date: 2025-09-10

Let’s say we 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: 2020-07-06
Another date: 2020-12-23
Difference in days: 170
Difference in weeks: 24
Difference in months: 5
Difference in years: 0

That’s all about manipulation on dates.

Source Code

Download

Thanks for reading.

Tags:

Leave a Reply

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