Java Language Simple Date Manipulations


Example

Get the current date.

LocalDate.now()

Get yesterday's date.

LocalDate y = LocalDate.now().minusDays(1);

Get tomorrow's date

LocalDate t = LocalDate.now().plusDays(1);

Get a specific date.

LocalDate t = LocalDate.of(1974, 6, 2, 8, 30, 0, 0);

In addition to the plus and minus methods, there are a set of "with" methods that can be used to set a particular field on a LocalDate instance.

LocalDate.now().withMonth(6);

The example above returns a new instance with the month set to June (this differs from java.util.Date where setMonth was indexed a 0 making June 5).

Because LocalDate manipulations return immutable LocalDate instances, these methods may also be chained together.

LocalDate ld = LocalDate.now().plusDays(1).plusYears(1);

This would give us tomorrow's date one year from now.