Java Calendar returns month in numeric integer format. To get month
name in string (Jan, Feb, Mar, …), we have to use SimpleDateFormat
class. First calendar date should convert into millisecond to parse into
date object. SimpleDateFormat returns this date in month name string.
Output
import java.util.Calendar;
import java.text.SimpleDateFormat;
public class MonthName {
public static void main(String[] args) {
Calendar ca1 = Calendar.getInstance();
// set(year, month, date) month 0-11
ca1.set(2009, 05, 25);
// int iMonth=ca1.get(Calendar.MONTH);
java.util.Date d = new java.util.Date(ca1.getTimeInMillis());
System.out.println("Month Name :"+new SimpleDateFormat("MMMM").format(d));
System.out.println("Month Name :"+new SimpleDateFormat("MMM").format(d));
}
}
Month Name in String:June
Month Name in String :Jun
Month Name in String :Jun
0 comments:
Post a Comment