How can I determine if there is a Joda DateTime, for example, between 4-8 p.m.

I would like to check if a given DateTime exists between 4:00 - 8:00 or 12:00 am. What would be the right way to do this?

This seems like a trick:

DateTime start = new DateTime().withHourOfDay(4); DateTime end = new DateTime().withHourOfDay(8); Interval interval = new Interval(start, end); if(interval.contains(now)) return true; 

Is there a better way?

+6
source share
2 answers

Just use getHourOfDay()

 int hour = new DateTime().getHourOfDay(); return ((hour >= 16) && (hour < 20)) //4-8pm || ((hour >= 0) && (hour < 3)); //12-3am 
+10
source

from his evening, the above condition ((hour >= 0) && (hour < 3)) not required.

0
source

Source: https://habr.com/ru/post/915820/


All Articles