我正在做一些火車時刻表的應用程序。我已經爲相應的時間表存儲了列車和時間。如果我選擇特定的火車名稱,我必須顯示相應火車的時間。現在我必須將我的數據庫時間與當前時間進行比較。我的數據庫時間,如果我的當前時間一小時後意味着我必須將其存儲到單獨的數組中,2小時後意味着我必須將其存儲到單獨的數組中。我的代碼在這裏。如何比較android中當前時間數據庫存儲時間?
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 now = 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 now ? : " + 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);
}
}
我得到了一些錯誤這樣的..
java.lang.IndexOutOfBoundsException:無效的指數12,大小爲12
我如何比較兩個時間?我該如何解決這個錯誤?有誰能夠幫助我?
你到目前爲止嘗試過什麼?你如何儲存時間?你說你把它存儲在一些「像這樣」的數組中 - 像什麼? –
我們所能說的只是:解決這個問題。 – ppeterka
有史以來最具描述性的變量名稱。 – Reinherd