Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parse exception in Android

I am using SimpleDateFormat class to parse date as follows :

SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

But, getting the Parse exception error as follows :

java.text.ParseException: Unparseable date: "2015-09-16 10:21 AM" (at offset 16)

I have checked another format as a argument in SimpleDateFormat class is : "yyyy-MM-dd HH:mm:ss a" but, still same error is coming.

Is there any other datetime format I have to pass as a argument of SimpleDateFormat class ?

like image 590
Jaimin Modi Avatar asked Sep 11 '26 07:09

Jaimin Modi


2 Answers

As you have said in the above comments, you use a TimePicker for choosing a date time and parse it. And you said that you also want the second. But apparently, TimePicker doesn't include the second. So how are you going to know what second the user wants? I guess 0. You can insert a string into the correct position to make it work. In you case you should insert ":00" in index 16. Just use a StringBuilder!

StringBuilder builder = new StringBuilder("2015-09-16 10:21 AM");
builder.insert (16, ":00");
like image 157
Sweeper Avatar answered Sep 13 '26 20:09

Sweeper


Try this if your input has not second value ..

 SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd hh:mm a");

And if it has second value also then use this

 SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss a");

otherwise above one

For More detail....

like image 24
MPG Avatar answered Sep 13 '26 22:09

MPG