Parse A Date From String Gives Exception In Android
I want to parse a string from string to date ,Date is of format like Tuesday March 19,2015. I want to parse and format it as yyyy-dd-mm format. The below code gives me exception th
Solution 1:
You must parse it into the date of your current format before format it to another date
DateFormatdf_parse=newSimpleDateFormat("EEEE MMM dd,yyyy");
Datedate_parse= df_parse.format(currentDate);
DateFormatdf1=newSimpleDateFormat("yyyy-MM-dd");
date1 = df1.parse(date_parse);
Solution 2:
I have created one common function for convert date format. You have to pass old Date format, new date format and date.
publicstaticStringconvertDateFormat(String oldFormat, String newFormat, String inputDate)
{
DateFormat theDateFormat = newSimpleDateFormat(oldFormat);
Date date = null;
try
{
date = theDateFormat.parse(inputDate);
}
catch (ParseException parseException)
{
// Date is invalid. Do what you want.
}
catch (Exception exception)
{
// Generic catch. Do what you want.
}
theDateFormat = newSimpleDateFormat(newFormat);
return theDateFormat.format(date).toString();
}
// Call funcation
StringconvertedDate= convertDateFormat("EEEE MMM dd,yyyy","yyyy-MM-dd",dateToConvert);
Post a Comment for "Parse A Date From String Gives Exception In Android"