I know there are lots of questions on SO about how to get Date
s in Java, but I want an example using new Java 8 Date
API. I also know about the JodaTime library, but I want a method without relying on external libraries.
The function needs to be compliant with these restrictions:
- Prevent errors from date savetime
- Inputs are two
Date
objects (without time, I know aboutLocalDateTime
, but I need to do this withDate
instances)
If you want logical calendar days, use
DAYS.between()
method fromjava.time.temporal.ChronoUnit
:If you want literal 24 hour days, (a duration), you can use the
Duration
class instead:For more information, refer to this document: Java SE 8 Date and Time.