Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to know if now time is between two hours?

Tags:

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:

enter image description here

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!! 
like image 881
Trancer Avatar asked Jun 20 '13 11:06

Trancer


People also ask

How to check a time is between two time in java?

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.

How do you check if a time is between two times in python?

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.

How do you check if a date lies between two dates in Java?

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.


1 Answers

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); 
like image 172
Evgeniy Dorofeev Avatar answered Sep 28 '22 09:09

Evgeniy Dorofeev