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.land. 👉 Use the Copy to mine functionality to copy this snippet to your own personal collection and easy manage your code snippets.

Subscribe to our newsletter for more code resources and news

Adrian Matei

Adrian Matei
Life force expressing itself as a coding capable human being

Default value for jax-rs query parameter

Default value for jax-rs query parameter code snippet Continue reading

Unmarshalling xml string to java object with JAXB

Published on February 20, 2022

Execute delete statement with jpa query

Published on February 19, 2022