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 ?
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");
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....
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With