我有一个字符串数组,其格式为“2012-04-26T08:38:00”。我需要两个时间差,所以我从“ T ”拆分字符串并将其存储在数组中 所以现在
at array index 0 = 2012-04-26
and array index 1 = 08:38:00
现在从第一个索引我用“:”拆分,所以它像
一样存储[0] = 08
[1] = 38
[2] = 00
现在,当我减去当前的时间时,它昨天给了我很好的输出,但现在它引起错误。
04-25 17:32:34.103: WARN/System.err(4292): java.lang.ArrayIndexOutOfBoundsException: index=1 length=1
04-25 17:32:34.103: WARN/System.err(4292): at com.housedisplay.ListViewCustomAdapter.getView(ListViewCustomAdapter.java:83)
04-25 17:32:34.103: WARN/System.err(4292): at android.widget.AbsListView.obtainView(AbsListView.java:1964)
04-25 17:32:34.113: WARN/System.err(4292): at android.widget.ListView.makeAndAddView(ListView.java:1756)
04-25 17:32:34.113: WARN/System.err(4292): at android.widget.ListView.fillDown(ListView.java:656)
04-25 17:32:34.113: WARN/System.err(4292): at android.widget.ListView.fillFromTop(ListView.java:716)
04-25 17:32:34.113: WARN/System.err(4292): at android.widget.ListView.layoutChildren(ListView.java:1609)
04-25 17:32:34.123: WARN/System.err(4292): at android.widget.AbsListView.onLayout(AbsListView.java:1794)
04-25 17:32:34.123: WARN/System.err(4292): at android.view.View.layout(View.java:9330)
04-25 17:32:34.123: WARN/System.err(4292): at android.view.ViewGroup.layout(ViewGroup.java:3795)
04-25 17:32:34.123: WARN/System.err(4292): at android.widget.RelativeLayout.onLayout(RelativeLayout.java:912)
04-25 17:32:34.123: WARN/System.err(4292): at android.view.View.layout(View.java:9330)
04-25 17:32:34.123: WARN/System.err(4292): at android.view.ViewGroup.layout(ViewGroup.java:3795)
04-25 17:32:34.123: WARN/System.err(4292): at android.widget.RelativeLayout.onLayout(RelativeLayout.java:912)
04-25 17:32:34.133: WARN/System.err(4292): at android.view.View.layout(View.java:9330)
04-25 17:32:34.133: WARN/System.err(4292): at android.view.ViewGroup.layout(ViewGroup.java:3795)
04-25 17:32:34.133: WARN/System.err(4292): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1542)
04-25 17:32:34.133: WARN/System.err(4292): at android.widget.LinearLayout.layoutHorizontal(LinearLayout.java:1527)
04-25 17:32:34.144: WARN/System.err(4292): at android.widget.LinearLayout.onLayout(LinearLayout.java:1316)
04-25 17:32:34.144: WARN/System.err(4292): at android.view.View.layout(View.java:9330)
04-25 17:32:34.144: WARN/System.err(4292): at android.view.ViewGroup.layout(ViewGroup.java:3795)
04-25 17:32:34.144: WARN/System.err(4292): at android.widget.FrameLayout.onLayout(FrameLayout.java:400)
04-25 17:32:34.144: WARN/System.err(4292): at android.view.View.layout(View.java:9330)
04-25 17:32:34.154: WARN/System.err(4292): at android.view.ViewGroup.layout(ViewGroup.java:3795)
04-25 17:32:34.154: WARN/System.err(4292): at com.google.android.maps.MapView.onLayout(MapView.java:764)
04-25 17:32:34.154: WARN/System.err(4292): at android.view.View.layout(View.java:9330)
04-25 17:32:34.163: WARN/System.err(4292): at android.view.ViewGroup.layout(ViewGroup.java:3795)
04-25 17:32:34.163: WARN/System.err(4292): at android.widget.RelativeLayout.onLayout(RelativeLayout.java:912)
04-25 17:32:34.163: WARN/System.err(4292): at android.view.View.layout(View.java:9330)
04-25 17:32:34.163: WARN/System.err(4292): at android.view.ViewGroup.layout(ViewGroup.java:3795)
04-25 17:32:34.163: WARN/System.err(4292): at android.widget.FrameLayout.onLayout(FrameLayout.java:400)
04-25 17:32:34.173: WARN/System.err(4292): at android.view.View.layout(View.java:9330)
04-25 17:32:34.183: WARN/System.err(4292): at android.view.ViewGroup.layout(ViewGroup.java:3795)
04-25 17:32:34.183: WARN/System.err(4292): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1542)
04-25 17:32:34.183: WARN/System.err(4292): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1403)
04-25 17:32:34.183: WARN/System.err(4292): at android.widget.LinearLayout.onLayout(LinearLayout.java:1314)
04-25 17:32:34.183: WARN/System.err(4292): at android.view.View.layout(View.java:9330)
04-25 17:32:34.183: WARN/System.err(4292): at android.view.ViewGroup.layout(ViewGroup.java:3795)
04-25 17:32:34.193: WARN/System.err(4292): at android.widget.FrameLayout.onLayout(FrameLayout.java:400)
04-25 17:32:34.193: WARN/System.err(4292): at android.view.View.layout(View.java:9330)
04-25 17:32:34.193: WARN/System.err(4292): at android.view.ViewGroup.layout(ViewGroup.java:3795)
04-25 17:32:34.203: WARN/System.err(4292): at android.view.ViewRoot.performTraversals(ViewRoot.java:1201)
04-25 17:32:34.203: WARN/System.err(4292): at android.view.ViewRoot.handleMessage(ViewRoot.java:1944)
04-25 17:32:34.203: WARN/System.err(4292): at android.os.Handler.dispatchMessage(Handler.java:99)
04-25 17:32:34.213: WARN/System.err(4292): at android.os.Looper.loop(Looper.java:126)
04-25 17:32:34.213: WARN/System.err(4292): at android.app.ActivityThread.main(ActivityThread.java:3997)
04-25 17:32:34.213: WARN/System.err(4292): at java.lang.reflect.Method.invokeNative(Native Method)
04-25 17:32:34.213: WARN/System.err(4292): at java.lang.reflect.Method.invoke(Method.java:491)
04-25 17:32:34.213: WARN/System.err(4292): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
04-25 17:32:34.223: WARN/System.err(4292): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
04-25 17:32:34.223: WARN/System.err(4292): at dalvik.system.NativeStart.main(Native Method)
代码::
try {
holder.imgViewLogo.setText(arr_ServiceNumber[position]);
holder.txtViewTitle.setText(itemList[position]);
stime = arr_ScheduledDepartureTime[position].split("T");
sTime2 = stime[1].split(":");
Date dt = new Date();
int hours = dt.getHours();
int minutes = dt.getMinutes();
int myhour = Integer.parseInt(sTime2[0]);
int mymin = Integer.parseInt(sTime2[1]);
int onefire = Math.abs((myhour - hours) * 60);
int total;
if (onefire >= 60) {
total = onefire - Math.abs((mymin - minutes));
} else {
total = onefire + Math.abs((mymin - minutes));
}
holder.lstrw_btn_departure.setText(total + " min \n" + "("
+ sTime2[0] + ":" + sTime2[1] + ")");
} catch (Exception e) {
e.printStackTrace();
}
答案 0 :(得分:1)
Java可以为你解析字符串。
String dateString = "2012-04-26T08:38:00";
SimpleDateFormat parser = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
Date date = null;
try {
date = parser.parse(dateString);
} catch (ParseException e) {
// error parsing.
}
if (date != null) {
// do something with date.
}
答案 1 :(得分:0)
这种情况正在发生,因为当您在ArrayList中只有1个元素时,您试图访问第二个索引(1),这意味着您唯一有效的索引是0。
错误的第一行告诉你这个......
`java.lang.ArrayIndexOutOfBoundsException: index=1 length=1`
这意味着在访问大小为1的ArrayList的索引号1时会遇到ArrayIndexOutOfBoundsException(意味着0是目前唯一包含内容的索引。)