Sometimes it might be needed to determine the beginning of the day, or the beginning and the end of tomorrow. It might be primarily needed for filtering purposes.
Using Java class
java.time.Instant, we can define a moment at any point in time. Class
Instant also ensures methods for arithmetic functions, which can be applied to the time value.
To instantiate the instance of class
Instant we can use static method
Instant now = Instant.now();
Now we can apply time shift functions as well as arithmetic functions. To get the beginning of the current day, we can apply truncate operation, like below:
Or if we want to get the beginning and the end of yesterday:
Instant yesterday = now.minus(1, ChronoUnit.DAYS); Instant beginOfYesterday = yesterday.truncatedTo(ChronoUnit.DAYS); Instant endOfYesterday = beginOfYesterday .plus(1, ChronoUnit.DAYS) .minus(1, ChronoUnit.NANOS);