Pergunta

Hello Friends i set time from time picker dialog so i want to check this time is in between specific range time or not.

My Time Range is 11:30 am to 10:00 pm

so i want to check that my enter time is in between 11:30 am to 10:00 pm or not.

my code is as below.

Calendar mCalendarOpeningTime;
Calendar mCalendarClosingTime;
Calendar finalTime;
 int hour,min;
private TimePickerDialog.OnTimeSetListener timePickerListener = new TimePickerDialog.OnTimeSetListener() {
    public void onTimeSet(TimePicker view, int selectedHour,
            int selectedMinute) {
        hour = selectedHour;
        min = selectedMinute;
        finalTime = Calendar.getInstance();
        finalTime.set(Calendar.HOUR_OF_DAY, selectedHour);
        finalTime.set(Calendar.MINUTE, selectedMinute);
        mStringGetTime = String.valueOf(DateFormat.format("h:mm aa",finalTime));


    }
};

And i set opening time and closing time value as follow

    mCalendarOpeningTime.set(11,30);
    mCalendarClosingTime.set(10,00);

In my submit button click i set following code

if(finalTime.after(mCalendarOpeningTime) && finalTime.before(mCalendarClosingTime))
                    {
                    System.out.println("Your time is in range");
                    }
                    else
                    {
                        System.out.println("Your time is not in range");
                    }

so when i submit it it print message every time as your time is not in range any idea how can i solve it?

Foi útil?

Solução

Instead of using Calendar instances for mCalendarOpeningTime and mCalendarClosingTime, why dont you use them as Date instances?? that'll be more efficient and might as well get your code up and running.

EDIT:- ok.. forget whats written above.. It was crap.. :P following the piece of code that will do what you want to achieve:-

First up.. set the mCalendarOpeningTime and mCalendarClosingTime something like this and call this method in onCreate() or onStart() so that the values are assigned to the respective "times" before you start using them:-

private void setOpeningAndClosingTimes() {
    mCalendarOpeningTime = Calendar.getInstance();
    mCalendarOpeningTime.set(Calendar.HOUR, 11);
    mCalendarOpeningTime.set(Calendar.MINUTE, 30);
    mCalendarClosingTime.set(Calendar.AM_PM, Calendar.AM);

    mCalendarClosingTime = Calendar.getInstance();
    mCalendarClosingTime.set(Calendar.HOUR, 10);
    mCalendarClosingTime.set(Calendar.MINUTE, 00);
    mCalendarClosingTime.set(Calendar.AM_PM, Calendar.PM);
}

your comparison is good enough.. try this and let me know if it worked or not...

Outras dicas

Try to implement your code in this way!!

import java.util.Calendar;

public class CompareTimestamp 

{

 public static void main(String args[])
 {

    Calendar firstLimit= Calendar.getInstance();
    firstLimit.set(Calendar.HOUR, 20);
    firstLimit.set(Calendar.MINUTE, 30);
    firstLimit.set(Calendar.SECOND, 30);

    Calendar secondLimit= Calendar.getInstance();
    secondLimit.set(Calendar.HOUR, 21);
    secondLimit.set(Calendar.MINUTE, 30);
    secondLimit.set(Calendar.SECOND, 30);



    if(finalTime .after(firstLimit)&&finalTime .before(secondLimit))
    {
        System.out.println("Its dinner time between 8:30 pm and 9:30pm");
    }
    else
    {
        System.out.println("No dinner right now");
    }

  }
  }

You can ask if you have any further queries :)

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top