Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if date is within time range using Joda or standard Java API

I have first time as string '12:00:00' and another '19:00:00'. How to check time portion of the day is within these time values?

like image 372
EugeneP Avatar asked May 25 '10 07:05

EugeneP


People also ask

How do you check if a date is within a date range 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.

What is Joda-time API?

Joda-Time is an API created by joda.org which offers better classes and having efficient methods to handle date and time than classes from java. util package like Calendar, Gregorian Calendar, Date, etc. This API is included in Java 8.0 with the java.

Is Joda-time deprecated?

So the short answer to your question is: YES (deprecated).

Does Joda DateTime have time zones?

Adjusting Time ZoneUse the DateTimeZone class in Joda-Time to adjust to a desired time zone. Joda-Time uses immutable objects. So rather than change the time zone ("mutate"), we instantiate a new DateTime object based on the old but with the desired difference (some other time zone). Use proper time zone names.


1 Answers

Using Joda Time you could do something like this:

DateTime start = new DateTime(2010, 5, 25, 12, 0, 0, 0);
DateTime end = new DateTime(2010, 5, 25, 21, 0, 0, 0);
Interval interval = new Interval(start, end);
DateTime test = new DateTime(2010, 5, 25, 16, 0, 0, 0);
System.out.println(interval.contains(test));
like image 64
Mark McLaren Avatar answered Sep 24 '22 13:09

Mark McLaren