Polar Bear
Polar Bear

Reputation: 131

How to Compare a Time with two other time provided?

Been confusing.. Like if we are comparing time, string is definitely not recommended... But if it is in the format of (HH:mm:ss). how should i compare them to do something?

For example: Target1: 9:00:00 Target2: 23:00:00

how to do the logic for comparison where the input is larger than Target1 and smaller than Target2?

if(input > Target1 && input < Target2){
  //do statement A
}else{
  //do statement B
}

so if my input time is 10:00:00, it should run statement A and if input time is 23:01:00, it should run statement B how should i do that? is larger than (>) and smaller than (<) appropriate in time format?

Upvotes: 2

Views: 345

Answers (3)

Ankitkumar Makwana
Ankitkumar Makwana

Reputation: 3485

you can calculate diffrent using calender function .getTimeInMillis(), and get diffrent of 2 diffrent time , here you need to set only your specific time in Calender and make comparision with it

try{
            Calendar calender = Calendar.getInstance();
            Calendar calDb = Calendar.getInstance();
            Calendar matchd = Calendar.getInstance();

            mYear = calender.get(Calendar.YEAR);
            mMonth = calender.get(Calendar.MONTH);
            mDay = calender.get(Calendar.DAY_OF_MONTH);

            mendYear = calDb.get(Calendar.YEAR);
            mendMonth = calDb.get(Calendar.MONTH);
            mendDay = calDb.get(Calendar.DAY_OF_MONTH);    
                       // Here you can change day values        
                    calDb.set(Calendar.DAY_OF_MONTH, mDay-1);

            strbeforedate = mDateFormat.format(calDb.getTime());
            curentdate = mDateFormat.format(calender.getTime());

            calDb.setTime(mDateFormat.parse(strbeforedate));
            calender.setTime(mDateFormat.parse(curentdate));



            String mydate = "2013.03.14 03:11";
            String mdatetime = "";

            deletepath =  new ArrayList<String>();          

                try{
                    // here your matching goes and pass date here 

                     matchd.setTime(mDateFormat.parse(mdatetime));
                     long diff = calDb.getTimeInMillis() - calender.getTimeInMillis();
                     long matchdiff = matchd.getTimeInMillis() - calender.getTimeInMillis();
                        if(diff < matchdiff){

                                     // do your work here
                        }else{

                         // do your else work here

                        }
                }catch(Exception e){
                    e.printStackTrace();
                }                   
        }

        }catch(Exception e){
            e.printStackTrace();
        }

    }

Upvotes: 0

Syamantak Basu
Syamantak Basu

Reputation: 935

SimpleDateFormat df=new SimpleDateFormat("hh:mm:ss");             
Date d1=df.parse(dateToPars); 

d1.after(otherTimeYouWantTocompare);  OR 
d1.before(otherTimeYouWantTocompare); 

But you have to provide the time in the mentioned format

Upvotes: 0

wangyif2
wangyif2

Reputation: 2863

Given them as string, you can convert them to a Date object from a SimpleDateFormat.

SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");

The easiest way is to convert them to the amount of milliseconds by doing

long time1 = sdf.parse(Target1).getTime();
long time2 = sdf.parse(Target2).getTime();
long inputTime = sdf.parse(input).getTime();

This way you are essentially doing a integer comparison, and you can forget about all the Date Time business.

if(inputTime > time1 && inputTime < time2)

Upvotes: 1

Related Questions