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!!
Method 1 :- Using SimpleDateFormat class and Date class Parse the Time Period in the format HH:MM:SS by creating a SimpleDateFormat object. SimpleDateFormat object parses the Time period and returns a Date object which can be used to calculate the time elapsed. Below is the code for the above approach: Java.
Therefore, you can use the expression start <= current <= end to check if a current time falls into the interval [start, end] when assuming that start , end , and current are datetime objects.
We can use the simple isBefore , isAfter and isEqual to check if a date is within a certain date range; for example, the below program check if a LocalDate is within the January of 2020. startDate : 2020-01-01 endDate : 2020-01-31 testDate : 2020-01-01 testDate is within the date range.
try this
int from = 2300; int to = 800; Date date = new Date(); Calendar c = Calendar.getInstance(); c.setTime(date); int t = c.get(Calendar.HOUR_OF_DAY) * 100 + c.get(Calendar.MINUTE); boolean isBetween = to > from && t >= from && t <= to || to < from && (t >= from || t <= to);
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