hosam hassn
hosam hassn

Reputation: 43

Test the current time does it match between two values?

I want to test current time. Is it the same between the two times?

-With the note, I've already done a job function to enter the start time and the time to end.

Now i want to complete the special function. i'm testing this entry value between the two times.

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState){
      super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        int hourStart = 5;
        int minuteStart = 30;
        String PMAMStart="AM";
        int hourEnd = 6;
        int minuteEnd = 30;
        String PMAMEnd="AM";

       boolean checkTime=  checkCurrentTimeBetweenTowVlue(hourStart,minuteStart,PMAMStart,hourEnd,minuteEnd,PMAMEnd);

    }


    private boolean checkCurrentTimeBetweenTowVlue(int hStart,int mStart,String ampmStart,int hEnd,int mEnd,String ampmEnd){

      Calendar cal = Calendar.getInstance();

      return false;
    }


}

Upvotes: 1

Views: 298

Answers (3)

deHaar
deHaar

Reputation: 18568

If you can use Java 8 (beginning with API level 26 in Android) or if you are willing / able / allowed to import a very useful library that enables java.time support to lower API levels, then the following might be a neat solution:

public static boolean isNowBetween(int startHour, int startMinute, int endHour, int endMinute) {
    LocalTime start = LocalTime.of(startHour, startMinute);
    LocalTime end = LocalTime.of(endHour, endMinute);
    LocalTime now = LocalTime.now();
    return now.isAfter(start) && now.isBefore(end);
}

Upvotes: 1

AJ7
AJ7

Reputation: 149

In your case, you can set the individual calendar fields such as AM_PM, Hour, and Minute for both start and end dates, convert the time to minutes, and then find the difference between the times. Here is the solution:

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState){
      super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        int hourStart = 5;
        int minuteStart = 30;
        String PMAMStart="AM";
        int hourEnd = 6;
        int minuteEnd = 30;
        String PMAMEnd="AM";

        boolean checkTime=  checkCurrentTimeBetweenTwoValues(hourStart,minuteStart,PMAMStart,hourEnd,minuteEnd,PMAMEnd);

        if (checkTime) {
            System.out.println("Time is Same");
        } else {
            System.out.println("Time is not Same");
        }

    }

    private boolean checkCurrentTimeBetweenTwoValues(int hStart,int mStart,String ampmStart,int hEnd,int mEnd,String ampmEnd){

      Calendar startCalendar = Calendar.getInstance();
      Calendar endCalendar = Calendar.getInstance();

      if (ampmStart.equalsIgnoreCase("AM")) {
          startCalendar.set(Calendar.AM_PM, 0);
      } else {
          startCalendar.set(Calendar.AM_PM, 1);
      }

      startCalendar.setTimeZone(TimeZone.getDefault());
      startCalendar.set(Calendar.HOUR, hStart);
      startCalendar.set(Calendar.MINUTE, mStart);

      if (ampmEnd.equalsIgnoreCase("AM")) {
          endCalendar.set(Calendar.AM_PM, 0);
      } else {
          endCalendar.set(Calendar.AM_PM, 1);
      }

      endCalendar.setTimeZone(TimeZone.getDefault());
      endCalendar.set(Calendar.HOUR, hEnd);
      endCalendar.set(Calendar.MINUTE, mEnd);

      long timeDifference = TimeUnit.MILLISECONDS.toMinutes(
            Math.abs(endCalendar.getTimeInMillis() - startCalendar.getTimeInMillis()));
      System.out.println(("Time Difference: " + timeDifference));

      return (timeDifference == 0);
    }
}

You can also use TimeUnit.MILLISECONDS.toSeconds to check for Seconds but for that, you need to set the Seconds component of the Calendar instance.

Regards, AJ

Upvotes: 1

SriDatta Yalla
SriDatta Yalla

Reputation: 866

Get current minutes and hours from cal object. Convert start, end, current time into minutes, then it will be easy to compare.

private boolean checkCurrentTimeBetweenTowVlue(int hStart,int mStart,String ampmStart,int hEnd,int mEnd,String ampmEnd){

  Calendar cal = Calendar.getInstance();
  int hours = cal.get(Calendar.HOUR_OF_DAY);
  int minutes = cal.get(Calendar.MINUTE); 

  if(ampmStart.equals("PM")){
     hStart += 12;
  }
  int minutes_start = hStart*60 + mStart; 

  if(ampmEnd.equals("PM")){
     hEnd += 12;
  }
  int minutes_end = hEnd*60 + mEnd; 

  int minutes_curr = hours*60 + minutes;


  if(minutes_curr>minutes_start && minutes_curr<minutes_end){
     return true;
  }

  return false;
}

Upvotes: 0

Related Questions