简体   繁体   中英

IllegalArgumentException: Parse error java.util.Date

I'm getting low rating on the app I published recently. The app crashes sometimes and users report this type of 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)

I'm getting reported this same error pretty frequently:

Parse error: Mon, 09 M

Parse error: Sat,

Parse error: Fri, 06 

Parse error: Thu,

Parse error: Tue, 03 May 2016 11

I really don't know how to handle this problem. I'm trying to display the time a news article was published.

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;
}

This line is indicated in the log: View listItem = listAdapter.getView(listPosition, null, this); (line 53)

I'm doing this way because, in my opinion, the app would look more beautiful if there is a "Yesterday" instead of "Thu, 12 May 2016 15:43:00 +0200"; or "2 days ago", ... and so on.

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;

}

This line is indicated in the log: _Date date_month = new Date(web.get(position).getPubdate());_ (line 81)

Could you help me please?

Edit

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();
    }

What is the returned type of getPubdate() method? Is it long or String?

Check if it's not a null and is long.

Update:

Since the returned date from web.get(position).getPubdate() is in a String format, you have to parse it to a Date object via SimpleDateFormat, ie:

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

You are calling the new Date(String s) constructor. Quoting javadoc:

Deprecated. As of JDK version 1.1 , replaced by DateFormat.parse(String s) .

Allocates a Date object and initializes it so that it represents the date and time indicated by the string s , which is interpreted as if by the parse(java.lang.String) method.

You are using a method that have been deprecated since 1997 . Deprecated means "that programmers are discouraged from using it, typically because it is dangerous, or because a better alternative exists ".

The error message says that getPubdate() returned the string Mon, 09 May . This definitely not a value that any Java parser can handle without an explicit format string. This includes the new Date(String s) call that you shouldn't be using anyway.

What kind of date value is that even supposed to be? There is no year!

Looking at the top of the stacktrace it looks like an incomplete string is being passed to the Date constructor, ie, there is no year and it's possibly illformed.

Try capturing what web.get(position).getPubdate() returns before calling the Date constructor.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM