user1320990
user1320990

Reputation:

Formatting date exceptions

I don't know why I`m getting this exception:

05-30 12:51:58.490: E/AndroidRuntime(3098): FATAL EXCEPTION: main 05-30 12:51:58.490: E/AndroidRuntime(3098): java.lang.IllegalArgumentException 05-30 12:51:58.490: E/AndroidRuntime(3098): at java.text.DateFormat.format(DateFormat.java:365) 05-30 12:51:58.490: E/AndroidRuntime(3098): at java.text.Format.format(Format.java:93) 05-30 12:51:58.490: E/AndroidRuntime(3098): at it.bloomp.helper.DateHelper.getDay(DateHelper.java:49) 05-30 12:51:58.490: E/AndroidRuntime(3098): at it.bloomp.helper.DateHelper.formatListViewItemDate(DateHelper.java:16) 05-30 12:51:58.490: E/AndroidRuntime(3098): at it.bloomp.list.EventsListAdapter.getView(EventsListAdapter.java:58) 05-30 12:51:58.490: E/AndroidRuntime(3098): at android.widget.AbsListView.obtainView(AbsListView.java:2033) 05-30 12:51:58.490: E/AndroidRuntime(3098): at android.widget.ListView.measureHeightOfChildren(ListView.java:1244) 05-30 12:51:58.490: E/AndroidRuntime(3098): at android.widget.ListView.onMeasure(ListView.java:1155) 05-30 12:51:58.490: E/AndroidRuntime(3098): at android.view.View.measure(View.java:12723) 05-30 12:51:58.490: E/AndroidRuntime(3098): at android.widget.RelativeLayout.measureChild(RelativeLayout.java:579) 05-30 12:51:58.490: E/AndroidRuntime(3098): at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:392) 05-30 12:51:58.490: E/AndroidRuntime(3098): at android.view.View.measure(View.java:12723) 05-30 12:51:58.490: E/AndroidRuntime(3098): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698) 05-30 12:51:58.490: E/AndroidRuntime(3098): at android.widget.FrameLayout.onMeasure(FrameLayout.java:293) 05-30 12:51:58.490: E/AndroidRuntime(3098): at android.view.View.measure(View.java:12723) 05-30 12:51:58.490: E/AndroidRuntime(3098): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698) 05-30 12:51:58.490: E/AndroidRuntime(3098): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1369) 05-30 12:51:58.490: E/AndroidRuntime(3098): at android.widget.LinearLayout.measureVertical(LinearLayout.java:660) 05-30 12:51:58.490: E/AndroidRuntime(3098): at android.widget.LinearLayout.onMeasure(LinearLayout.java:553) 05-30 12:51:58.490: E/AndroidRuntime(3098): at android.view.View.measure(View.java:12723) 05-30 12:51:58.490: E/AndroidRuntime(3098): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698) 05-30 12:51:58.490: E/AndroidRuntime(3098): at android.widget.FrameLayout.onMeasure(FrameLayout.java:293) 05-30 12:51:58.490: E/AndroidRuntime(3098): at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2092) 05-30 12:51:58.490: E/AndroidRuntime(3098): at android.view.View.measure(View.java:12723) 05-30 12:51:58.490: E/AndroidRuntime(3098): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1064) 05-30 12:51:58.490: E/AndroidRuntime(3098): at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2442) 05-30 12:51:58.490: E/AndroidRuntime(3098): at android.os.Handler.dispatchMessage(Handler.java:99) 05-30 12:51:58.490: E/AndroidRuntime(3098): at android.os.Looper.loop(Looper.java:137) 05-30 12:51:58.490: E/AndroidRuntime(3098): at 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): at java.lang.reflect.Method.invoke(Method.java:511) 05-30 12:51:58.490: E/AndroidRuntime(3098): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784) 05-30 12:51:58.490: E/AndroidRuntime(3098): at 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);
    }
}

Upvotes: 0

Views: 164

Answers (3)

Jon Lin
Jon Lin

Reputation: 143896

Don't you think it would be easier to just use date.get(Calendar.DAY_OF_MONTH) and append a "0" to the front if it's less than 10? SimpleDateFormat is a little expensive.

Upvotes: 0

jt.
jt.

Reputation: 7705

As @Michael stated, you are passing a Calendar object to a method that expects a Date object. This is an unfortunate side effect of the design of the java.text.Format class. It has a method named format that accepts an Object. This is likely why your editor/compiler is not complaining. Modify your code to get a Date object from the Calendar by calling getTime(). For example:

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

Upvotes: 1

Michael
Michael

Reputation: 35341

The DateFormat.format() method takes a Date object as an argument, not a Calendar object.

Upvotes: 4

Related Questions