0

我不知道为什么会收到此异常:

05-30 12:51:58.490:E/AndroidRuntime(3098):致命异常:主要 05-30 12:51:58.490:E/AndroidRuntime(3098):java.lang.IllegalArgumentException 05-30 12:51:58.490: E/AndroidRuntime(3098): 在 java.text.DateFormat.format(DateFormat.java:365) 05-30 12:51:58.490: E/AndroidRuntime(3098): 在 java.text.Format.format(Format.java :93) 05-30 12:51:58.490: E/AndroidRuntime(3098): 在 it.bloomp.helper.DateHelper.getDay(DateHelper.java:49) 05-30 12:51:58.490: E/AndroidRuntime(3098 ): 在 it.bloomp.helper.DateHelper.formatListViewItemDate(DateHelper.java:16) 05-30 12:51:58.490: E/AndroidRuntime(3098): 在 it.bloomp.list.EventsListAdapter.getView(EventsListAdapter.java: 58) 05-30 12:51:58.490: E/AndroidRuntime(3098): 在 android.widget.AbsListView.obtainView(AbsListView.java:2033) 05-30 12:51:58.490:E/AndroidRuntime(3098): 在 android.widget.ListView.measureHeightOfChildren(ListView.java:1244) 05-30 12:51:58.490: E/AndroidRuntime(3098): 在 android.widget.ListView.onMeasure(ListView.java :1155) 05-30 12:51:58.490: E/AndroidRuntime(3098): 在 android.view.View.measure(View.java:12723) 05-30 12:51:58.490: E/AndroidRuntime(3098):在 android.widget.RelativeLayout.measureChild(RelativeLayout.java:579) 05-30 12:51:58.490: E/AndroidRuntime(3098): 在 android.widget.RelativeLayout.onMeasure(RelativeLayout.java:392) 05-30 12 :51:58.490: E/AndroidRuntime(3098): 在 android.view.View.measure(View.java:12723) 05-30 12:51:58.490: E/AndroidRuntime(3098): 在 android.view.ViewGroup。 measureChildWithMargins(ViewGroup.java:4698) 05-30 12:51:58.490: E/AndroidRuntime(3098): 在 android.widget.FrameLayout。onMeasure(FrameLayout.java:293) 05-30 12:51:58.490: E/AndroidRuntime(3098): 在 android.view.View.measure(View.java:12723) 05-30 12:51:58.490: E/ AndroidRuntime(3098): 在 android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698) 05-30 12:51:58.490: E/AndroidRuntime(3098): 在 android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1369 ) 05-30 12:51:58.490: E/AndroidRuntime(3098): 在 android.widget.LinearLayout.measureVertical(LinearLayout.java:660) 05-30 12:51:58.490: E/AndroidRuntime(3098): 在 android .widget.LinearLayout.onMeasure(LinearLayout.java:553) 05-30 12:51:58.490: E/AndroidRuntime(3098): 在 android.view.View.measure(View.java:12723) 05-30 12:51 :58.490: E/AndroidRuntime(3098): 在 android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698) 05-30 12:51:58.490:E/AndroidRuntime(3098): 在 android.widget.FrameLayout.onMeasure(FrameLayout.java:293) 05-30 12:51:58.490: E/AndroidRuntime(3098): 在 com.android.internal.policy.impl.PhoneWindow $DecorView.onMeasure(PhoneWindow.java:2092) 05-30 12:51:58.490: E/AndroidRuntime(3098): 在 android.view.View.measure(View.java:12723) 05-30 12:51:58.490 : E/AndroidRuntime(3098): 在 android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1064) 05-30 12:51:58.490: E/AndroidRuntime(3098): 在 android.view.ViewRootImpl.handleMessage(ViewRootImpl. java:2442) 05-30 12:51:58.490: E/AndroidRuntime(3098): 在 android.os.Handler.dispatchMessage(Handler.java:99) 05-30 12:51:58.490: E/AndroidRuntime(3098) : 在 android.os.Looper.loop(Looper.java:137) 05-30 12:51:58.490: E/AndroidRuntime(3098): 在 android.app.ActivityThread。main(ActivityThread.java:4424) 05-30 12:51:58.490: E/AndroidRuntime(3098): at java.lang.reflect.Method.invokeNative(Native Method) 05-30 12:51:58.490: E/AndroidRuntime (3098): 在 java.lang.reflect.Method.invoke(Method.java:511) 05-30 12:51:58.490: E/AndroidRuntime(3098): 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller。运行(ZygoteInit.java:784)05-30 12:51:58.490:E/AndroidRuntime(3098):在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)05-30 12:51: 58.490:E/AndroidRuntime(3098):在 dalvik.system.NativeStart.main(本机方法)E/AndroidRuntime(3098): 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784) 05-30 12:51:58.490: E/AndroidRuntime(3098): 在 com.android.internal .os.ZygoteInit.main(ZygoteInit.java:551) 05-30 12:51:58.490: E/AndroidRuntime(3098): at dalvik.system.NativeStart.main(Native Method)E/AndroidRuntime(3098): 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784) 05-30 12:51:58.490: E/AndroidRuntime(3098): 在 com.android.internal .os.ZygoteInit.main(ZygoteInit.java:551) 05-30 12:51:58.490: E/AndroidRuntime(3098): at dalvik.system.NativeStart.main(Native Method)

package it.bloomp.helper;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;


public class DateHelper {

    public String formatListViewItemDate(String dateString) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar date = Calendar.getInstance();

        try {
            date.setTime(simpleDateFormat.parse(dateString));
            String day = getDay(date);
            String weekDay = getWeekDay(date);
            String month = getMonth(date);
            String year = getYear(date);

            Calendar now = Calendar.getInstance();

            if (date.get(Calendar.YEAR) == now.get(Calendar.YEAR)) {
                if (date.before(now)) {
                    return "agora";
                } else if (date.get(Calendar.DAY_OF_YEAR) == now.get(Calendar.DAY_OF_YEAR)) {
                    return "hoje";
                } else if (date.getTimeInMillis() - now.getTimeInMillis() < 86400000) {
                    return "amanhã";
                } else if (date.get(Calendar.WEEK_OF_YEAR) == now.get(Calendar.WEEK_OF_YEAR)) {
                    return weekDay;
                } else if (date.get(Calendar.MONTH) == now.get(Calendar.MONTH)) {
                    return weekDay + " " + day;
                } else {
                    return month + " " + day;
                }
            } else {
                return year + " " + month;
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }

        return null;
    }

    public String getDay(Calendar date) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd");
        return simpleDateFormat.format(date);
    }

    public String getWeekDay(Calendar date) {
        if (date.get(Calendar.DAY_OF_WEEK) == 0) {
            return "dom";
        } else if (date.get(Calendar.DAY_OF_WEEK) == 1) {
            return "seg";
        } else if (date.get(Calendar.DAY_OF_WEEK) == 2) {
            return "ter";
        } else if (date.get(Calendar.DAY_OF_WEEK) == 3) {
            return "qua";
        } else if (date.get(Calendar.DAY_OF_WEEK) == 4) {
            return "qui";
        } else if (date.get(Calendar.DAY_OF_WEEK) == 5) {
            return "sex";
        } else if (date.get(Calendar.DAY_OF_WEEK) == 6) {
            return "sab";
        }

        return null;
    }

    public String getMonth(Calendar date) {
        if (date.get(Calendar.MONTH) == 0) {
            return "jan";
        } else if (date.get(Calendar.MONTH) == 1) {
            return "fev";
        } else if (date.get(Calendar.MONTH) == 2) {
            return "mar";
        } else if (date.get(Calendar.MONTH) == 3) {
            return "abr";
        } else if (date.get(Calendar.MONTH) == 4) {
            return "mai";
        } else if (date.get(Calendar.MONTH) == 5) {
            return "jun";
        } else if (date.get(Calendar.MONTH) == 6) {
            return "jul";
        } else if (date.get(Calendar.MONTH) == 7) {
            return "ago";
        } else if (date.get(Calendar.MONTH) == 8) {
            return "set";
        } else if (date.get(Calendar.MONTH) == 9) {
            return "out";
        } else if (date.get(Calendar.MONTH) == 10) {
            return "nov";
        } else if (date.get(Calendar.MONTH) == 11) {
            return "dez";
        }

        return null;
    }

    public String getYear(Calendar date) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy");
        return simpleDateFormat.format(date);
    }
}
4

3 回答 3

4

DateFormat.format()方法将Date对象作为参数,而不是Calendar对象。

于 2012-05-30T19:24:05.870 回答
1

正如@Michael 所说,您正在将一个 Calendar 对象传递给一个需要 Date 对象的方法。这是 java.text.Format 类设计的一个不幸的副作用。它有一个名为 format 的方法,它接受一个对象。这可能是您的编辑器/编译器不抱怨的原因。修改您的代码以通过调用 getTime() 从日历中获取 Date 对象。例如:

public String getDay(Calendar date) {
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd");
    return simpleDateFormat.format(date.getTime());
}
于 2012-05-30T19:30:25.670 回答
0

date.get(Calendar.DAY_OF_MONTH)如果小于 10,您不认为只使用并在前面附加一个“0”会更容易吗?SimpleDateFormat有点贵。

于 2012-05-30T19:30:28.640 回答