How do you get the date/time range for "today" using the Joda date/time library in Java?

Viewed 28139

Assuming this is how you get the current time in Joda time:

DateTime now = new DateTime();

How do you calculate values for the variables dateTimeAtStartOfToday and dateTimeAtEndOfToday?

What I'm trying to do is generate some SQL to do a lookup of all transactions that have occurred between the startOfToday and endOfToday.

6 Answers

As a Kotlin extension function it looks like this:

fun DateTime.isOnSameDay(timeOnDayToCheck: DateTime) =
  timeOnDayToCheck.toLocalDate().toInterval(this.zone).contains(this)

This does not contain times equaling the end of the day (start is included) so maybe add an "or"-case with interval.getEnd().isEqual(this)).

Related