IllegalArgumentException:解析错误java.util.Date

时间:2016-05-12 20:31:33

标签: java android date parse-error

我对最近发布的应用的评分很低。应用程序有时会崩溃,用户会报告此类错误:

java.lang.IllegalArgumentException: Parse error: Mon, 09 May 
at java.util.Date.parseError(Date.java:364)
at java.util.Date.parse(Date.java:560)
at java.util.Date.<init>(Date.java:154)
at newproject.CustomList.getView(CustomList.java:81)
at newproject.NestedListView.onMeasure(NestedListView.java:53)
at android.view.View.measure(View.java:17637)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5536)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1436)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:722)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:613)
at android.view.View.measure(View.java:17637)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5536)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:436)
at android.view.View.measure(View.java:17637)
at android.support.v4.widget.NestedScrollView.measureChildWithMargins(NestedScrollView.java:1415)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:436)
at android.support.v4.widget.NestedScrollView.onMeasure(NestedScrollView.java:482)
at android.view.View.measure(View.java:17637)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5536)
at android.support.design.widget.CoordinatorLayout.onMeasureChild(CoordinatorLayout.java:610)
at android.support.design.widget.HeaderScrollingViewBehavior.onMeasureChild(HeaderScrollingViewBehavior.java:78)
at android.support.design.widget.AppBarLayout$ScrollingViewBehavior.onMeasureChild(AppBarLayout.java:1132)
at android.support.design.widget.CoordinatorLayout.onMeasure(CoordinatorLayout.java:675)
at android.view.View.measure(View.java:17637)
at android.support.v4.widget.SwipeRefreshLayout.onMeasure(SwipeRefreshLayout.java:600)
at android.view.View.measure(View.java:17637)
at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:728)
at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:464)
at android.view.View.measure(View.java:17637)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5536)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:436)
at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:135)
at android.view.View.measure(View.java:17637)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5536)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1436)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:722)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:613)
at android.view.View.measure(View.java:17637)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5536)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:436)
at android.view.View.measure(View.java:17637)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5536)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1436)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:722)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:613)
at android.view.View.measure(View.java:17637)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5536)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:436)
at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2618)
at android.view.View.measure(View.java:17637)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2019)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1177)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1383)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1065)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5901)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:767)
at android.view.Choreographer.doCallbacks(Choreographer.java:580)
at android.view.Choreographer.doFrame(Choreographer.java:550)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:753)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:211)
at android.app.ActivityThread.main(ActivityThread.java:5371)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:945)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:740)

我经常报告同样的错误:

Parse error: Mon, 09 M

Parse error: Sat,

Parse error: Fri, 06 

Parse error: Thu,

Parse error: Tue, 03 May 2016 11

我真的不知道如何处理这个问题。我正在尝试显示新闻文章的发布时间。

NestedListView

public class NestedListView extends ListView implements View.OnTouchListener, AbsListView.OnScrollListener {

private int listViewTouchAction;
private static final int MAXIMUM_LIST_ITEMS_VIEWABLE = 99;

public NestedListView(Context context, AttributeSet attrs) {
    super(context, attrs);
    listViewTouchAction = -1;
    setOnScrollListener(this);
    setOnTouchListener(this);
}

@Override
public void onScroll(AbsListView view, int firstVisibleItem,
                     int visibleItemCount, int totalItemCount) {
    if (getAdapter() != null && getAdapter().getCount() > MAXIMUM_LIST_ITEMS_VIEWABLE) {
        if (listViewTouchAction == MotionEvent.ACTION_MOVE) {
            scrollBy(0, -1);
        }
    }
}

@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
}

@SuppressLint("DrawAllocation")
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);

    int newHeight = 0;
    final int heightMode = MeasureSpec.getMode(heightMeasureSpec);
    int heightSize = MeasureSpec.getSize(heightMeasureSpec);
    if (heightMode != MeasureSpec.EXACTLY) {
        ListAdapter listAdapter = getAdapter();
        if (listAdapter != null && !listAdapter.isEmpty()) {
            int listPosition;
            for (listPosition = 0; listPosition < listAdapter.getCount()
                    && listPosition < MAXIMUM_LIST_ITEMS_VIEWABLE; listPosition++) {
                View listItem = listAdapter.getView(listPosition, null, this);
                //now it will not throw a NPE if listItem is a ViewGroup instance
                if (listItem instanceof ViewGroup) {
                    listItem.setLayoutParams(new LayoutParams(
                            LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
                }
                listItem.measure(widthMeasureSpec, heightMeasureSpec);
                newHeight += listItem.getMeasuredHeight();
            }
            newHeight += getDividerHeight() * listPosition;
        }
        if ((heightMode == View.MeasureSpec.AT_MOST) && (newHeight > heightSize)) {
            if (newHeight > heightSize) {
                newHeight = heightSize;
            }
        }
    } else {
        newHeight = getMeasuredHeight();
    }
    setMeasuredDimension(getMeasuredWidth(), newHeight);
}

@Override
public boolean onTouch(View v, MotionEvent event) {
    if (getAdapter() != null && getAdapter().getCount() > MAXIMUM_LIST_ITEMS_VIEWABLE) {
        if (listViewTouchAction == MotionEvent.ACTION_MOVE) {
            scrollBy(0, 1);
        }
    }
    return false;
}

此行显示在日志中:查看listItem = listAdapter.getView(listPosition,null,this); (第53行)

我这样做是因为,在我看来,如果有一个“Yesterday”而不是“Thu,2016年5月12日15:43:00 +0200”,应用程序看起来会更漂亮;或“2天前”,......等等。

CustomList

public class CustomList extends ArrayAdapter<RSSItem> {

private static Activity context = null;
private final List<RSSItem> web;

public CustomList(Activity context, List<RSSItem> web) {
    super(context, R.layout.new_listview, web);
    CustomList.context = context;
    this.web = web;
}

@SuppressLint("SetTextI18n")
@Override
public View getView(final int position, View view, ViewGroup parent) {
    LayoutInflater inflater = context.getLayoutInflater();
    @SuppressLint({"ViewHolder", "InflateParams"}) final View rowView = inflater.inflate(R.layout.new_listview, null, true);

    ImageView imageView = (ImageView)rowView.findViewById(R.id.image);
    Picasso.with(context).load(web.get(position).getImage()).into(imageView);

    TextView textView = (TextView)rowView.findViewById(R.id.title);
    Typeface typeface = Typeface.createFromAsset(context.getAssets(), "RobotoSlab-Regular.ttf");
    textView.setTypeface(typeface);
    textView.setText(Html.fromHtml(web.get(position).getTitle()));

    TextView textView2 = (TextView)rowView.findViewById(R.id.pubdate);

    /** Months **/

    @SuppressLint("SimpleDateFormat") DateFormat df_month = new SimpleDateFormat("MM");
    Date date_month = new Date(web.get(position).getPubdate());
    String pubdate_month = df_month.format(date_month);

    Calendar cal_month = Calendar.getInstance();
    @SuppressLint("SimpleDateFormat") DateFormat month_date = new SimpleDateFormat("MM");
    String month_name = month_date.format(cal_month.getTime());

    @SuppressLint("SimpleDateFormat") DateFormat df_p_month = new SimpleDateFormat("MM");
    Calendar cal_p_month = Calendar.getInstance();
    cal_p_month.add(Calendar.MONTH, -1);
    String date_p_month = df_p_month.format(cal_p_month.getTime());


    /** Days **/

    @SuppressLint("SimpleDateFormat") DateFormat df_day = new SimpleDateFormat("dd");
    Date date_day = new Date((web.get(position).getPubdate());
    String pubdate_day = df_day.format(date_day);

    @SuppressLint("SimpleDateFormat") DateFormat df_0 = new SimpleDateFormat("dd");
    Calendar cal_0 = Calendar.getInstance();
    cal_0.add(Calendar.DAY_OF_YEAR, -0);
    String date_0 = df_0.format(cal_0.getTime());

    @SuppressLint("SimpleDateFormat") DateFormat df_1 = new SimpleDateFormat("dd");
    Calendar cal_1 = Calendar.getInstance();
    cal_1.add(Calendar.DAY_OF_YEAR, -1);
    String date_1 = df_1.format(cal_1.getTime());

    @SuppressLint("SimpleDateFormat") DateFormat df_2 = new SimpleDateFormat("dd");
    Calendar cal_2 = Calendar.getInstance();
    cal_2.add(Calendar.DAY_OF_YEAR, -2);
    String date_2 = df_2.format(cal_2.getTime());

    @SuppressLint("SimpleDateFormat") DateFormat df_3 = new SimpleDateFormat("dd");
    Calendar cal_3 = Calendar.getInstance();
    cal_3.add(Calendar.DAY_OF_YEAR, -3);
    String date_3 = df_3.format(cal_3.getTime());

    @SuppressLint("SimpleDateFormat") DateFormat df_4 = new SimpleDateFormat("dd");
    Calendar cal_4 = Calendar.getInstance();
    cal_4.add(Calendar.DAY_OF_YEAR, -4);
    String date_4 = df_4.format(cal_4.getTime());

    @SuppressLint("SimpleDateFormat") DateFormat df_5 = new SimpleDateFormat("dd");
    Calendar cal_5 = Calendar.getInstance();
    cal_5.add(Calendar.DAY_OF_YEAR, -5);
    String date_5 = df_5.format(cal_5.getTime());


    if(pubdate_month.equalsIgnoreCase(month_name)){

        if(pubdate_day.equalsIgnoreCase(date_0)){
            @SuppressLint("SimpleDateFormat") DateFormat df = new SimpleDateFormat("HH:mm");
            Date date = new Date(String.valueOf(Html.fromHtml(web.get(position).getPubdate())));
            String pubdate = df.format(date);
            textView2.setText(pubdate);
        }else if(pubdate_day.equalsIgnoreCase(date_1)){
            textView2.setText("Yesterday");
        }else if(pubdate_day.equalsIgnoreCase(date_2)){
            textView2.setText("2 days ago");
        }else if(pubdate_day.equalsIgnoreCase(date_3)){
            textView2.setText("3 days ago");
        }else if(pubdate_day.equalsIgnoreCase(date_4)){
            textView2.setText("4 days ago");
        }else if(pubdate_day.equalsIgnoreCase(date_5)){
            textView2.setText("5 days ago");
        }else if(pubdate_month.equalsIgnoreCase(date_p_month)){
            textView2.setText("1 month ago");
        }

    } else {
        textView2.setText("Today");
    }

    return rowView;

}

此行显示在日志中:_Date date_month = new Date(web.get(position).getPubdate()); _(第81行)

你能帮帮我吗?

修改

TextView textView2 = (TextView)rowView.findViewById(R.id.pubdate);

    SimpleDateFormat sdf = new SimpleDateFormat("EEE, dd MMM yyyy h:mm:ss Z");
    try {
        Date date = sdf.parse(web.get(position).getPubdate());

        /** Months **/

        DateFormat df_month = new SimpleDateFormat("MM");
        String pubdate_month = df_month.format(date);

        Calendar cal_month = Calendar.getInstance();
        DateFormat month_date = new SimpleDateFormat("MM");
        String month_name = month_date.format(cal_month.getTime());

        DateFormat df_p_month = new SimpleDateFormat("MM");
        Calendar cal_p_month = Calendar.getInstance();
        cal_p_month.add(Calendar.MONTH, -1);
        String date_p_month = df_p_month.format(cal_p_month.getTime());

        /** Days **/

        DateFormat df_day = new SimpleDateFormat("dd");
        String pubdate_day = df_day.format(date);

        DateFormat df_0 = new SimpleDateFormat("dd");
        Calendar cal_0 = Calendar.getInstance();
        cal_0.add(Calendar.DAY_OF_YEAR, -0);
        String date_0 = df_0.format(cal_0.getTime());

        DateFormat df_1 = new SimpleDateFormat("dd");
        Calendar cal_1 = Calendar.getInstance();
        cal_1.add(Calendar.DAY_OF_YEAR, -1);
        String date_1 = df_1.format(cal_1.getTime());

        DateFormat df_2 = new SimpleDateFormat("dd");
        Calendar cal_2 = Calendar.getInstance();
        cal_2.add(Calendar.DAY_OF_YEAR, -2);
        String date_2 = df_2.format(cal_2.getTime());

        DateFormat df_3 = new SimpleDateFormat("dd");
        Calendar cal_3 = Calendar.getInstance();
        cal_3.add(Calendar.DAY_OF_YEAR, -3);
        String date_3 = df_3.format(cal_3.getTime());

        DateFormat df_4 = new SimpleDateFormat("dd");
        Calendar cal_4 = Calendar.getInstance();
        cal_4.add(Calendar.DAY_OF_YEAR, -4);
        String date_4 = df_4.format(cal_4.getTime());

        DateFormat df_5 = new SimpleDateFormat("dd");
        Calendar cal_5 = Calendar.getInstance();
        cal_5.add(Calendar.DAY_OF_YEAR, -5);
        String date_5 = df_5.format(cal_5.getTime());

        if(pubdate_month.equalsIgnoreCase(month_name)){
            if(pubdate_day.equalsIgnoreCase(date_0)){
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm");
                String date1 = simpleDateFormat.parse(web.get(position).getPubdate()).toString();
                textView2.setText(date1);
            }else if(pubdate_day.equalsIgnoreCase(date_1)){
                textView2.setText("Yesterday");
            }else if(pubdate_day.equalsIgnoreCase(date_2)){
                textView2.setText("2 days ago");
            }else if(pubdate_day.equalsIgnoreCase(date_3)){
                textView2.setText("3 days ago");
            }else if(pubdate_day.equalsIgnoreCase(date_4)){
                textView2.setText("4 days ago");
            }else if(pubdate_day.equalsIgnoreCase(date_5)){
                textView2.setText("5 days ago");
            }else if(pubdate_day.isEmpty()){
                textView2.setText("Today");
            }else if(pubdate_month.isEmpty()){
                textView2.setText("1 week ago");
            }else if(pubdate_month.equalsIgnoreCase(date_p_month)){
                textView2.setText("1 month ago");
            }
        } else {
            textView2.setText("Today");
        }

    } catch (ParseException e) {
        e.printStackTrace();
    }

3 个答案:

答案 0 :(得分:1)

返回的getPubdate()方法是什么? 是长还是字符串?

检查它是否为空并且很长。

更新

由于web.get(position).getPubdate()的返回日期是String格式,因此必须通过SimpleDateFormat将其解析为Date对象,即:

SimpleDateFormat sdf = new SimpleDateFormat("EEE, dd MMM yyyy h:mm:ss Z");
Date date = sdf.parse(web.get(position).getPubdate());

答案 1 :(得分:1)

您正在调用new Date(String s)构造函数。引用javadoc:

  

<强> 已过时。自JDK 1.1版开始 ,由DateFormat.parse(String s)替换。

     

分配一个Date对象并对其进行初始化,使其表示字符串s指示的日期和时间,该字符串被parse(java.lang.String)方法解释为。

您使用的方法自1997年以来已被弃用 。不推荐意味着“程序员不鼓励使用它,通常是因为它很危险,或者因为存在更好的替代方案”。

错误消息显示getPubdate()返回了字符串Mon, 09 May。这绝对不是任何Java解析器在没有显式格式字符串的情况下可以处理的值。这包括您不应该使用的new Date(String s)电话。

甚至应该是什么样的日期值?没有一年!

答案 2 :(得分:0)

查看堆栈跟踪的顶部,它看起来像一个不完整的字符串被传递给Date构造函数,即,没有年份,它可能是错误的。

尝试在调用Date构造函数之前捕获web.get(position).getPubdate()返回的内容。