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

Answers


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

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


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?


Are you just miss a ")" in

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

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


Need Your Help

How to modify the keyboard input in QT?

c++ qt text keyboard-events abbreviation

The following feature needs to be implemented to our existing QT & C++ application.

Is copying a file byte for byte using Java equivalent to copying the entire file?

java sockets video javafx

I am working on a P2P Video sharing application. I am reading contents of the file(a .mp4 video) byte-for-byte using the RandomAccessFile class and I am transferring chunks of these bytes over a ne...