0

我采用了一个字符串数组,它以“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();
        }
4

2 回答 2

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.
}
于 2012-04-25T12:21:22.643 回答
0

发生这种情况是因为您在 ArrayList 中只有 1 个元素时尝试访问第二个索引 (1),这意味着您唯一的有效索引是 0。

错误的第一行告诉您这一点..

`java.lang.ArrayIndexOutOfBoundsException: index=1 length=1`

这意味着您在为大小为 1 的 ArrayList 访问索引号 1 时遇到了 ArrayIndexOutOfBoundsException(这意味着 0 是目前唯一包含某些内容的索引。)

于 2012-04-25T12:09:33.470 回答