What is the simplest way to use Joda time and get something like the following behavior:
public boolean check( DateTime checkTime ) { DateTime someTime = new DateTime(); //set to "now" for sake of example return checkTime.isBefore( someTime ) && checkTime.notMoreThanThreeHoursBefore( someTime ); }
So if someTime
is set to 15:00 and checkTime
is set to 12:15 it should return true.
If someTime
is set to 15:00 and checkTime
is set to 11:45 it should return false because checkTime
is more than 3 hours before someTime
.
If someTime
is set to 15:00 and checkTime
is set to 15:15 it should return false because checkTime
is after someTime
.
The idea is quite simple, just use Calendar class to roll the month back and forward to create a “date range”, and use the Date. before() and Date. after() to check if the Date is within the range.
An interval in Joda-Time represents an interval of time from one instant to another instant. Both instants are fully specified instants in the datetime continuum, complete with time zone.
Joda-Time provides a comprehensive formatting system. There are two layers: High level - pre-packaged constant formatters. Mid level - pattern-based, like SimpleDateFormat.
After some playing around, I found this which reads nicely:
return new Interval( someTime.minusHours( 3 ), someTime ).contains( checkTime );
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With