Reputation: 841
I am trying to convert my month into the MMM format using SimpleDateFormat but i am unable to convert it.
tvDisplayDate = (TextView) findViewById(R.id.dateDisplay);
Calendar cal=Calendar.getInstance();
SimpleDateFormat format = new SimpleDateFormat("MMM");
int tempyear = cal.get(Calendar.YEAR);
int tempmonth = cal.get(Calendar.MONTH);
int tempday = cal.get(Calendar.DAY_OF_MONTH);
String month = new Integer(tempmonth).toString();
Upvotes: 2
Views: 2173
Reputation: 46768
The following works (I just tested it)
Calendar cal=Calendar.getInstance();
int tempmonth = cal.get(Calendar.MONTH);
SimpleDateFormat newformat = new SimpleDateFormat("MMM");
SimpleDateFormat oldformat = new SimpleDateFormat("MM");
String monthName = null;
Date myDate;
try {
myDate = oldformat.parse(String.valueOf(tempmonth));
monthName = newformat.format(myDate);
} catch (ParseException e) {
e.printStackTrace();
}
Log.d("MMM", monthName);
Upvotes: 4
Reputation: 490
I'd say use Joda Time, would make such things easier.
Anyway, this question here has several answers.
Upvotes: 0
Reputation: 938
You are not actually using the format you created. Try:
String month = format.format(tempmonth);
You can format the whole date at once with:
String dateString = sdf.format(cal.getTime());
Upvotes: 0