Java Language Dates and Time (java.time.*) Simple Date Manipulations

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Extensions
> Step 2: And Like the video. BONUS: You can also share it!

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.



Got any Java Language Question?