java - How to compare current time database stored times in android? -


i doing train schedule application. have stored trains , timings corresponding schedule. if selecting particular train name have show timings of corresponding train. have compare data base times current time. db time if after 1 hour of current time means have store separate array , after 2 hours means have store separate array. , code here.

     arraylist<string> str_arr1 = new arraylist<string>();  arraylist<string> str_arr2 = new arraylist<string>();  arraylist<integer> hr_arr_1 = new arraylist<integer>();  arraylist<integer> mint_arr_1 = new arraylist<integer>();  arraylist<integer> hr_arr_2 = new arraylist<integer>();  arraylist<integer> mint_arr_2 = new arraylist<integer>();        string str,str1;  final arraylist<hashmap<string, string>> new_item = new arraylist<hashmap<string, string>>();   int[] str_int = new int[3];  int[] str_int1 = new int[3];    @suppresslint("newapi")    public class sheduleactivitymain extends activity    {      int count=0;  public void oncreate(bundle savedinstancestate)   {      simpledateformat dateformat = new simpledateformat("hh:mm:ss"); calendar = calendar.getinstance(); calendar now1 = calendar.getinstance(); calendar now2 = calendar.getinstance();    system.out.println("current time : " + dateformat.format(now.gettime()));    string time_now = dateformat.format(now.gettime());    system.out.println("time_now----->"+time_now);    now1.add(calendar.hour,1);     system.out.println("new time after adding 1 hours : "                      + dateformat.format(now1.gettime()));    now2.add(calendar.hour,2);     system.out.println("new time after adding 2 hours : "                      + dateformat.format(now2.gettime()));    system.out.println("is now1 after ? : " + now1.after(now));     stringtokenizer st1 = new stringtokenizer(time_now, ":");    while (st1.hasmoreelements())     {         //system.out.println(st1.nextelement());         str = (string) st1.nextelement();         str_arr1.add(str);     }          str_int[0] = integer.parseint(str_arr1.get(0));        str_int[1] = integer.parseint(str_arr1.get(1));        str_int[2] = integer.parseint(str_arr1.get(2));      for(int j=0;j<str_int.length;j++)         {          system.out.println("integer array is... "+j+"..."+str_int[j]);         }          for(int time=0;time<train_time.length;time++)        {              stringtokenizer st2 = new stringtokenizer(train_time[time], ":");              while (st2.hasmoreelements())             {                 str1 = (string) st2.nextelement();                 str_arr2.add(str1);                 system.out.println("str1..."+str1);              }                 system.out.println("str_arr2.........."+str_arr2);      system.out.println("method calling..........");         gettraintime(str_arr2.get(count),str_arr2.get(count+1));         for(int chk=count;chk<=str_arr2.size()-3;chk++)        {            gettraintime(str_arr2.get(count),str_arr2.get(count+1));        }  }       void gettraintime(string s1,string s2)     {         system.out.println("count--->"+count);         int n1 = integer.parseint(s1);         int n2 = integer.parseint(s2);         system.out.println("n1.n2  "+n1+n2);         count = count+3;         system.out.println(str_int[0]+str_int[1]);         if(n1==str_int[0]+1)         {             system.out.println("inside if condition.....");             hr_arr_1.add(n1);             //mint_arr_1.add(n2);             system.out.println("hr_arr_1,mint_arr_1"+hr_arr_1+mint_arr_1);          }         else if(n1==str_int[0]+2 )         {          }         //system.out.println("hr_arr_1,mint_arr_1"+hr_arr_1+mint_arr_1);        }   } 

i got error this..

java.lang.indexoutofboundsexception: invalid index 12, size 12

how can compare 2 times? how can resolve error? can me?

java.lang.indexoutofboundsexception: invalid index 12, size 12 

this error means, try value @ 13th position, array has 12 entries. (-> index ouf of bounds). keep in mind: index of array starts 0!

you should use sth yourarray[yourarray.length - 1]

edit: , please modify code next time. it`s unreadable (remove unnecessary system.out.print, give variables describing name...)

by way, check if value within next 2 hours, can use calendar objects like

calendar currenttime = calendar.getinstance();  calendar currenttimeplus2 = calendar.getinstance(); currenttimeplus2.add(calendar.hour,2);  calendar yourdatatime; //where ever might come  if( yourdatatime.gettime().before(currenttimeplus2.gettime()) &&      yourdatatime.gettime().after(currenttime.gettime()){      //your data within next 2 hours } else{     //your data not within next 2 hours } 

)


Comments

Popular posts from this blog

c++ - CryptStringToBinary API behavior -

c++ - Correct method for redrawing a layered window -

java.util.scanner - How to read and add only numbers to array from a text file -