Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Joda, How do I determine if a given date is between time x and a time 3 hours earlier than x?

Tags:

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.

like image 344
Alex B Avatar asked Jan 12 '11 22:01

Alex B


People also ask

How do I check if a date is within a certain range in Java?

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.

Does Joda DateTime have time zones?

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.

What is Joda-time format?

Joda-Time provides a comprehensive formatting system. There are two layers: High level - pre-packaged constant formatters. Mid level - pattern-based, like SimpleDateFormat.


1 Answers

After some playing around, I found this which reads nicely:

return new Interval( someTime.minusHours( 3 ), someTime ).contains( checkTime ); 
like image 89
Alex B Avatar answered Sep 28 '22 18:09

Alex B