How to know if now time is between two hours? How to know if now time is between two hours? android android

How to know if now time is between two hours?


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);


Calendar cal = Calendar.getInstance(); //Create Calendar-Objectcal.setTime(new Date());               //Set the Calendar to nowint hour = cal.get(Calendar.HOUR_OF_DAY); //Get the hour from the calendarif(hour <= 23 && hour >= 8)              // Check if hour is between 8 am and 11pm{     // do whatever you want}


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.

ZoneId z = ZoneId.of( "America/Montreal" );

Get your current moment.

ZonedDateTime zdt = ZonedDateTime.now( z );

Extract the time-of-day. The Local part of the name means there is no concept of time zone contained within the object.

LocalTime lt = zdt.toLocalTime();

Define the limits of the evening.

LocalTime start = LocalTime.of( 23 , 0 );  // 11 PM.LocalTime stop = LocalTime.of( 8 , 0 );  // 8 AM.

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.

Boolean silentRunning = null ;if( start.equals( stop ) ) {    silentRunning = Boolean.FALSE ;} else if( stop.isAfter( start ) ) {  // Example 3 PM to 6 PM.    silentRunning = ( ! lt.isBefore( start ) ) && lt.isBefore( stop ) ;} else if ( stop.isBefore( start ) ) {  // Example 11 PM to 8 AM.    silentRunning = ( lt.equals( start ) || lt.isAfter( start ) ) && lt.isBefore( stop ) ;} else {    // Error. Should not reach this point. Paranoid check.}

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.

DateTimeZone zone = DateTimeZone.forID( "America/Montreal" );DateTime now = DateTime.now( zone );Integer hour = now.getHourOfDay();Boolean isNight = ( ( hour >= 23  ) && ( hour < 8 ) );