0

These lines of codes

SimpleDateFormat formatter = new SimpleDateFormat("dd/mm/yy"); 

     ContentValues values = new ContentValues();
        values.put(COL_EVENT_ID, appointment.mEventId);
        try {
            values.put(COL_START_DATE, String.valueOf(formatter.parse(appointment.mStartDate.toString())));
            values.put(COL_END_DATE, String.valueOf(formatter.parse(appointment.mEndDate.toString())));

        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

causees this exception

10-15 11:44:38.150: WARN/System.err(3861): java.text.ParseException: Unparseable date: "Mon Jan 10 00:10:00 GMT+02:00 2011"

what is the possible solution ?

Adham
  • 63,550
  • 98
  • 229
  • 344

2 Answers2

6

Your format is completely wrong. Not only are you using mm (which means minutes) when you probably meant MM, but this:

Mon Jan 10 00:10:00 GMT+02:00 2011

is clearly not in the format

dd/MM/yy

You probably want something like

EEE MMM dd HH:mm:ss z yyyy

EDIT: That works for me in desktop Java:

import java.text.*;

public class Test {

    public static void main(String[] args) throws ParseException {
        String value = "Mon Jan 10 00:10:00 GMT+02:00 2011";
        String pattern = "EEE MMM dd HH:mm:ss z yyyy";
        DateFormat format = new SimpleDateFormat(pattern);
        System.out.println(format.parse(value));
    }
}

You may want to set the culture of the SimpleDateFormat of course.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
0

java.time

The java.util Date-Time API and their formatting API, SimpleDateFormat are outdated and error-prone. It is recommended to stop using them completely and switch to the modern Date-Time API*.

Solution using java.time, the modern Date-Time API:

import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Locale;

public class Main {
    public static void main(String[] args) {
        String strDateTime = "Mon Jan 10 00:10:00 GMT+02:00 2011";
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("EEE MMM d H:m:s OOOO uuuu", Locale.ENGLISH);
        ZonedDateTime zdt = ZonedDateTime.parse(strDateTime, dtf);
        System.out.println(zdt);
    }
}

Output:

ONLINE DEMO

Notes:

  1. m specifies minute-of-hour whereas M specifies month-of-year. You have wrongly used the former.
  2. The pattern used with the parser (DateTimeFormatter or SimpleDateFormat) should match the input date-time string. Your pattern, dd/mm/yy is off by far.
  3. Never use SimpleDateFormat or DateTimeFormatter without a Locale.

Learn more about the modern Date-Time API from Trail: Date Time.


* For any reason, if you have to stick to Java 6 or Java 7, you can use ThreeTen-Backport which backports most of the java.time functionality to Java 6 & 7. If you are working for an Android project and your Android API level is still not compliant with Java-8, check Java 8+ APIs available through desugaring and How to use ThreeTenABP in Android Project.

Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110