Got a requirement that means you have to get a new date and time object by adding or subtracting an amount of time from an existing date and time object? If so, then you're in luck.

It's easy to do that in your Java application.

And in this guide, I'll walk you through it.

Plus and Minus

Fortunately, the Java date and time library offers a couple of intuitive methods for adding and subtracting time units to an existing LocalDateTime, LocalTime, or LocalDate object.

The methods are called plus() and minus(), respectively.

I'll show you how to use them.

Let's start by instantiating a LocalDateTime object:

LocalDateTime localDateTime = LocalDateTime.now();

That's going to give you today's date at the current time.

You can add or subtract any time unit from nanoseconds up to millenia by using methods on that object. 

For example:

LocalDateTime otherDate = localDateTime.minus(31, ChronoUnit.DAYS);

That's going to give you a new date and time that's 31 days ago.

The minus() and plus() methods accept two parameters.

The first parameter is the number that you want to add or subtract. 

The second parater is the type of units that you want to add or subtract.

So with the example above: the first parameter (31) put together with the second parameter (ChronoUnit.DAYS) means subtract 31 days from that LocalDateTime object.

If you used the plus() method instead of the minus() method above and left everything else the same, that would add 31 days to that LocalDateTime object.

About That ChronoUnit

ChronoUnit is an enum that offers a set of units you can use to manipulate dates and times.

Now, you might think that second parameter in those plus() and minus() methods is defined as the ChronoUnit enum.

But it's not.

It's actually a TemporalUnit interface.

But ChromeUnit implements TemporalUnit. So that's why you can use it as the second parameter in the plus() and minus() methods.

This brings up an interesting side-quest: did you know that enums can implement interfaces? I dare say that even a lot of experienced Java developers don't know that.

Another Way

There's another way to use the minus() method if you'd prefer to stick to one parameter instead of two. Use Period.

The Period class represents an amount of time in the calendar system. It's designed for dates.

The Period equivalent for time on the clock is Duration.

If you want to use minus() with Period, just do it like this:

LocalDateTime otherDate = localDateTime.minus(Period.ofDays(31));

That will give you the same outcome as the code above.

Some Convenience Methods

The LocalDateTime class also offers several convenience methods you can use to add and subtract time units:

  • minusDays(long days)
  • minusHours(long hours)
  • minusMinutes(long minutes)
  • minusMonths(long months)
  • minusNanos(long nanos)
  • minusSeconds(long seconds)
  • minusWeeks(long weeks)
  • minusYears(long years)
  • plusDays(long days)
  • plusHours(long hours)
  • plusMinutes(long minutes)
  • plusMonths(long months)
  • plusNanos(long nanos)
  • plusSeconds(long seconds)
  • plusWeeks(long weeks)
  • plusYears(long years)

All of those methods are self-explanatory.

Unsurprisingly, though, the time-specific methods are only available on LocalTime and LocalDateTime. 

And the date-specific methods are only available on LocalDate and LocalDateTime.

Wrapping It Up

That was easy, wasn't it?

Now you know how to take a LocalDate or LocalDateTime object and manipulate it to get a different date and time object.

Feel free to put what you've learned here into your own Java applications.

Have fun!

Photo by Bich Tran from Pexels