Maximus1809
Maximus1809

Reputation: 585

Java - Format Date

i have a problem to format a Date-String into a specific format.

Here is my code:

                    Calendar c = Calendar.getInstance();
                    SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
                    // Get current Date in right format...
                    String formattedDateToday = df.format(c.getTime());

                    try {
                        // Get the date of the event in the right format ...
                        SimpleDateFormat dff = new SimpleDateFormat(
                                "yyyy-MM-dd");

                        String formattedDateEvent = dff
                                .format(event.Datum_Von);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }

The format of "event.Datum_Von" is "dd.MM.yyyy".

I get this exception (at: String formattedDateEvent = dff .format(event.Datum_Von);)

My intention is to compare the date of "event.Datum_Von" with the current date.

10-04 15:56:09.895: W/System.err(9480): java.lang.IllegalArgumentException 10-04 15:56:09.910: W/System.err(9480): at java.text.DateFormat.format(DateFormat.java:365) 10-04 15:56:09.915: W/System.err(9480): at java.text.Format.format(Format.java:93)

Upvotes: 0

Views: 190

Answers (1)

Prasad
Prasad

Reputation: 240

Try this out

public void formatDate() {
        try {
            Calendar c = Calendar.getInstance();
            SimpleDateFormat df = new SimpleDateFormat("MM.dd.yyyy");
            // Get current Date in right format...
            Date date = df.parse("10.12.2013");

            // Get the date of the event in the right format ...
            SimpleDateFormat dff = new SimpleDateFormat("yyyy-MM-dd");

            String formattedDateEvent = dff.format(date);
            Log.v(">>>>>>>>", formattedDateEvent);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

If you know what format of date you are passing then you can convert that in DATE and reformat to required way

Upvotes: 1

Related Questions