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 ⭐🙏

Subscribe to our newsletter for more code resources and news

Adrian Matei (aka adixchen)

Adrian Matei (aka adixchen)
Life force expressing itself as a coding capable human being

Mongoose schema field from list of strings (enum)

Mongoose schema field from list of strings (enum) code snippet Continue reading

Set value of input field in angular template

Published on December 14, 2022

How to use jest test.each function

Published on December 14, 2022