maddy
maddy

Reputation: 216

date parsing error in android

here is the code

public static final String DATE_TIME_FORMAT = "yyyy-MM-dd kk:mm:ss";
public static final String KEY_DATE_TIME = "reminder_date_time";
SimpleDateFormat datetimeFormat = new SimpleDateFormat(DATE_TIME_FORMAT) ;

    try {
    String dateString = reminder.getString(reminder.getColumnIndexOrThrow(ReminderDbAdapter.KEY_DATE_TIME));

    Date date =  (Date) datetimeFormat.parse(reminder.getString(dateString);

    mCalendar.setTime(date); 
    } catch (java.text.ParseException e) {
        Log.e("edit_activity", e.getMessage(), e);
    }

it is not parsing date as all the values are fetched but date is not parsed so logcat is showing an error.

error::06-11 14:08:59.320: E/edit_activity(361): java.text.ParseException: Unparseable date: 2012-03-11 13:14:49

pleaseee help me out

Upvotes: 0

Views: 1600

Answers (4)

whb_zju
whb_zju

Reputation: 133

Are you just miss a ")" in

Date date =  (Date) datetimeFormat.parse(reminder.getString(dateString);

Can you just print reminder.getString(dateString), does it right?

Upvotes: 0

jeet
jeet

Reputation: 29199

There is no need to give 'T', in DateFormat, instead you can specify SimpleDateFormat as below:

public static final String DATE_TIME_FORMAT = "yyyy-MM-dd kk:mm:ss";

kk = Hours in 1-24 format
hh= hours in 1-12 format
KK= hours in 0-11 format
HH= hours in 0-23 format

Upvotes: 2

Aleks G
Aleks G

Reputation: 57316

You declared DATE_TIME_FORMAT = "yyyy-MM-dd'T'kk:mm:ss", however your time is in format "yyyy-MM-dd kk:mm:ss" - hence it can't parse it. Remove 'T' from your format - and you'll be fine.

I just tested with this code:

public static final String DATE_TIME_FORMAT = "yyyy-MM-dd kk:mm:ss";
public static final String KEY_DATE_TIME = "reminder_date_time";

SimpleDateFormat datetimeFormat = new SimpleDateFormat(DATE_TIME_FORMAT) ;

try {
    String dateString = "2012-03-11 13:14:49";
    Date date =  (Date) datetimeFormat.parse(dateString);
    System.out.println(date);

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

And it worked just fine, printing Sun Mar 11 13:14:49 GMT 2012

Upvotes: 2

mihail
mihail

Reputation: 2173

remove the T character from your string, then use

public static final String DATE_TIME_FORMAT = "yyyy-MM-dd hh:mm:ss";

are you sure about these kk's?

Upvotes: 0

Related Questions