I have a now time:
new Date();
And I have some hour constants, for example, 23 and 8 (it's 11pm or 23:00, 8am or 08:00). How I can know is now time between it's two hour constants?
It need to run some code of program or not to run if now time is between in two hours, for example, do not run some code if its already evening and while it is not a morning.
Here the image to better explain:
Some situations when silent mode does not fire:
00:00 20.06.13 - 23:00 20.06.13 // after 23.00 can loud!!
23:00 20.06.13 - 15:00 20.06.13 // after 15.00 can loud!!
01:00 20.06.13 - 08:00 20.06.13 // after 08.00 can loud!!
21:00 20.06.13 - 08:00 20.06.13 // after 08.00 can loud!!
You could also convert your input string to an integer and compare it against your constants. This way you don't even need to work with the Calendar and Date objects.
java.time
The modern way is with the java.time classes built into Java 8 and later. Much of the functionality is back-ported to Java 6 & 7 in the ThreeTen-Backport project and further adapted to Android in ThreeTenABP project.
Time zone is crucial here. For any given moment, the date and time-of-day both vary around the world by zone.
Get your current moment.
Extract the time-of-day. The
Local
part of the name means there is no concept of time zone contained within the object.Define the limits of the evening.
Compare.
We need to figure out if we are straddling over a new day or within the same day. A
LocalTime
has no concept of date, only a single generic day of 24 hours. So we must test if the start is before or after the stop as we need different comparison algorithm for each case. And we should consider if the start equals the stop, as that may be a special case depending on your business rules.In date-time work, we usually define spans of time as Half-Open, where the beginning is inclusive while the ending is exclusive.
Here's one way to do it.
About java.time
The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as
java.util.Date
,Calendar
, &SimpleDateFormat
.The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.
To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.
Where to obtain the java.time classes?
UPDATE: The above is a later version of this Answer. Below is the old.
Joda-Time
The Joda-Time library is vastly superior to the java.util.Date and .Calendar classes for date-time work.
Time zone is crucial for determine the time of day. Obviously "now" is later in the day in Paris than Montréal.
Definig a range of time is usually best done as half-open,
[)
, where the beginning is inclusive but the ending is exclusive.You can see a tutorial here with
Date.before
and you can do withDate.after
Also you can get his milliseconds and compare it.
I think that this is more cleaner solution and it`s works. I have tested it with different time parameters.
here is a function that checks is now(current time) is either between 1 to 4 OR 4 to 8 OR 8 to 12 OR 12 to 16 OR 16 to 20 OR 20 to 1 And returns next accuring time.