Minus duration in Java local datetime from now example
Get current day with LocalDateTime.now()
from which you can substract years, weeks, days up to nanos (in the shown example days with minusDays(long days)
):
LocalDateTime dateToLookBack = LocalDateTime.now().minusDays(30);
You can achieve the same result by using the minus(TemporalAmount amountToSubtract)
method as below
final var dateToLookBackTo = LocalDateTime.now().minus(Duration.ofDays(30));
Reference - https://docs.oracle.com/javase/8/docs/api/java/time/LocalDateTime.html
Shared with from Codever.
👉 Use the Copy to mine
functionality to copy this snippet to your own personal collection and easy manage
your code snippets.
Codever is open source on Github ⭐🙏