Jef
Jef

Reputation: 811

Check if time is inbetween timerange in Android

I am trying to check if the current time is in the range of a specified range. I made a method to check this, but it doesn't work. I'm not sure why not and how to get it to work.

private Calendar fromTime;
private Calendar toTime;
private Calendar currentTime;

public boolean checkTime(String time) {
    try {
        String[] times = time.split("-");
        String[] from = times[0].split(":");
        String[] until = times[1].split(":");

        fromTime = Calendar.getInstance();
        fromTime.set(Calendar.HOUR, Integer.valueOf(from[0]));
        fromTime.set(Calendar.MINUTE, Integer.valueOf(from[1]));

        toTime= Calendar.getInstance();
        toTime.set(Calendar.HOUR, Integer.valueOf(until[0]));
        toTime.set(Calendar.MINUTE, Integer.valueOf(until[1]));

        currentTime = Calendar.getInstance();       
        currentTime.set(Calendar.HOUR, Calendar.HOUR_OF_DAY);
        currentTime.set(Calendar.MINUTE, Calendar.MINUTE);
        if(currentTime.after(fromTime) && currentTime.before(toTime)){
            return true;
        }
     } catch (Exception e) {
         return false;
     }
    return false;
}

I am trying to test it like this:

if(checkTime("06:00-19:00")){
    inRange = true;
}

The NPE is gone, but it's still not calculating if the time is in the range of fromTime to toTime. Any help is very much appreciated!

Upvotes: 4

Views: 3563

Answers (6)

Munna
Munna

Reputation: 3

 private boolean checkBedTime(String time) {
    // Time Pattern Like : "06:00-19:00"
    String[] times = time.split("-");
    String[] from = times[0].split(":");
    String[] until = times[1].split(":");

    int fromHour = Integer.parseInt(from[0]);
    int fromMinute = Integer.parseInt(from[1]);

    int toHour = Integer.parseInt(until[0]);
    int toMinute = Integer.parseInt(until[1]);

    Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(System.currentTimeMillis());
    int currentHour = calendar.get(Calendar.HOUR_OF_DAY);
    int currentMinute = calendar.get(Calendar.MINUTE);

    int convertedFromMinute = (fromHour * 60) + fromMinute;
    int convertedToMinute = (toHour * 60) + toMinute;
    int convertedCurrentMinute = (currentHour * 60) + currentMinute;


    if (convertedFromMinute == convertedToMinute) {
        Toast.makeText(context, "Sleep Time & Wake Up Time can't be same", Toast.LENGTH_SHORT).show();
        return false;

    } else if (convertedToMinute < convertedFromMinute) {
        convertedToMinute = convertedToMinute + (24 * 60);
    }
    Log.v("Time", "FromMinute --> " + convertedFromMinute);
    Log.v("Time", "ToMinute --> " + convertedToMinute);
    Log.v("Time", "CurrentMinute -- > " + convertedCurrentMinute);
    if (convertedCurrentMinute >= convertedFromMinute && convertedCurrentMinute <= convertedToMinute) {
        return true;
    } else {
        return false;
    }


}

Upvotes: 0

Sagar Adhikari
Sagar Adhikari

Reputation: 19

Put currentTime outside of setTime and remove if-statement from setTime. If you need put if-statement outside of setTime.

Upvotes: 0

nakul
nakul

Reputation: 85

SimpleDateFormat format = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss"); SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");

    Calendar cal = Calendar.getInstance();
    Calendar cal2 = Calendar.getInstance();



    //cal.add(Calendar.DATE, 1);
    String date = sdf.format(cal.getTime());
    String dateStart = date+" "+"11:30:00";

    String date2 = sdf.format(cal2.getTime());
    String dateStop =  date2+" "+"23:00:00";

    Calendar calendar = Calendar.getInstance();
    String currentTime =  format.format(calendar.getTime());



    Date d1 = null;
    Date d2 = null;
    Date d3 = null;


    try {
        d1 = format.parse(dateStart);

    d2 = format.parse(dateStop);
        d3 = format.parse(currentTime);
       // Toast.makeText(getApplicationContext(),""+dateCal,Toast.LENGTH_SHORT).show();
        if (d3.before(d2)
                && d3.after(d1) ){}else{} } catch (ParseException e) {
        e.printStackTrace();
    }

Upvotes: 1

njzk2
njzk2

Reputation: 39397

this

    currentTime.set(Calendar.HOUR, Calendar.HOUR_OF_DAY);
    currentTime.set(Calendar.MINUTE, Calendar.MINUTE);

Doesn't do what you would like it to do. What it does is set the field HOUR to the value of Calendar.HOUR, which is an arbitrary constant.

You don't need those 2 lines as getInstance returns a Calendar at the current time.

Upvotes: 1

Gem
Gem

Reputation: 1606

You have not initialized toTime and fromTime objects before using them. So better call toTime = Calendar.getInstance(); before.

private Calendar fromTime;
private Calendar toTime;
private Calendar currentTime;

public boolean checkTime(String time) {
try {
    String[] times = time.split("-");
    String[] from = times[0].split(":");
    String[] until = times[1].split(":");

    fromTime = Calendar.getInstance();
    fromTime.set(Calendar.HOUR, Integer.valueOf(from[0]));
    fromTime.set(Calendar.MINUTE, Integer.valueOf(from[1]));

    toTime = Calendar.getInstance();
    toTime.set(Calendar.HOUR, Integer.valueOf(until[0]));
    toTime.set(Calendar.MINUTE, Integer.valueOf(until[1]));

    currentTime = Calendar.getInstance();
    currentTime.set(Calendar.HOUR, Calendar.HOUR_OF_DAY);
    currentTime.set(Calendar.MINUTE, Calendar.MINUTE);
    if(currentTime.after(fromTime) && currentTime.before(toTime)){
        return true;
    }
    } catch (Exception e) {
        return false;
    }
    return false;
}

Upvotes: 2

Jorgesys
Jorgesys

Reputation: 126455

Initialize variables and change the return type of your method to boolean.

private Calendar fromTime;
private Calendar toTime;
private Calendar currentTime;

public boolean checkTime(String time) {
try {
    String[] times = time.split("-");
    String[] from = times[0].split(":");
    String[] until = times[1].split(":");

    fromTime = Calendar.getInstance();
    fromTime.set(Calendar.HOUR_OF_DAY, Integer.valueOf(from[0]));
    fromTime.set(Calendar.MINUTE, Integer.valueOf(from[1]));

    toTime = Calendar.getInstance();
    toTime.set(Calendar.HOUR_OF_DAY, Integer.valueOf(until[0]));
    toTime.set(Calendar.MINUTE, Integer.valueOf(until[1]));

    currentTime = Calendar.getInstance();
    currentTime.set(Calendar.HOUR_OF_DAY, Calendar.HOUR_OF_DAY);
    currentTime.set(Calendar.MINUTE, Calendar.MINUTE);
    if(currentTime.after(fromTime) && currentTime.before(toTime)){
        return true;
    }
    } catch (Exception e) {
        return false;
    }
    return false;
}

Upvotes: 5

Related Questions