Issue
I'm trying to get a short month name by passing the month int to a function. But it always returns 'Jan'
Here is the function:
public static String getMonthName_Abbr(int month) {
Calendar cal = Calendar.getInstance();
cal.set(Calendar.MONTH, month);
SimpleDateFormat month_date = new SimpleDateFormat("MMM");
String month_name = month_date.format(month);
return month_name;
}
Solution
You simply need to pass cal.getTime()
to your format
call, rather than month
itself.
See the working demo at http://ideone.com/kKrGY9
I am not sure why your code worked as given, seeing as how format
expects a Date
and not an int
; however, if somehow it did, perhaps the value of month
, being a small integer was interpreted as a time around the epoch (January 1, 1970). Just a thought, but at any rate, your function will work with that one small change.
Answered By - Ray Toal
Answer Checked By - Mary Flores (JavaFixing Volunteer)