Date format conversion Android
I have string form of date:
2011-03-27T09:39:01.607
and I want to format it to March 27, 2011
I am using
DateFormat[] formats = new DateFormat[] {
DateFormat.getDateInstance(), DateFormat.getDateTimeInstance(),
DateFormat.getTimeInstance(), };
String actDate= formats[0].format(uploadeddate.substring(0,9));
but its not working.
How do I convert to March 27, 2011
?
try this
SimpleDateFormat form = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS");
java.util.Date date = null;
try
{
date = form.parse("2011-03-27T09:39:01.607");
}
catch (ParseException e)
{
e.printStackTrace();
}
SimpleDateFormat postFormater = new SimpleDateFormat("MMMMM dd, yyyy");
String newDateStr = postFormater.format(date);
now newDateStr = March 27, 2011;
May be this is of any help;
String convertDate(String inputDate) {
DateFormat theDateFormat = new SimpleDateFormat("yyyy-MM-dd");
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 = new SimpleDateFormat("MMM dd, yyyy");
return theDateFormat.format(date);
}
You can use android.text.format.DateFormat
in this way:
DateFormat.getMediumDateFormat(context).format(date);
String dateimput = "2011-03-27T09:39:01.607"
SimpleDateFormat formatrecived = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
SimpleDateFormat formarwanted = new SimpleDateFormat("MMMM dd, yyyy");
Date recived = formatrecived.parse(dateimput);
Date output = formatwanted.format(recived);
Note: dateimput you need to format to yyyy-MM-dd hh:mm:ss
using replace and substring