Name of the month as a string

I am trying to return the month name as String, for example, "May", "September", "November".

I tried:

int month = c.get(Calendar.MONTH); 

However, this returns integers (5, 9, 11, respectively). How to get the name of the month?

+61
android calendar
May 31 '11 at 19:52
source share
10 answers

Use getDisplayName .

For earlier use of the API String.format(Locale.US,"%tB",c);

+76
May 31 '11 at 19:57
source share

Use this:

 Calendar cal=Calendar.getInstance(); SimpleDateFormat month_date = new SimpleDateFormat("MMMM"); String month_name = month_date.format(cal.getTime()); 

The name of the month will contain the full name of the month, if you want to use the short name of the month it is

  SimpleDateFormat month_date = new SimpleDateFormat("MMM"); String month_name = month_date.format(cal.getTime()); 
+116
Dec 19 '11 at 3:19 a.m.
source share

To get the month in a string variable, use the code below

For example, the month of September:

M β†’ 9

MM β†’ 09

MMM β†’ Sep

MMMM β†’ September

 String monthname=(String)android.text.format.DateFormat.format("MMMM", new Date()) 
+69
Jan 31 '14 at 13:39
source share
 SimpleDateFormat dateFormat = new SimpleDateFormat( "LLLL", Locale.getDefault() ); dateFormat.format( date ); 

For some languages ​​(e.g. Russian), this is the only correct way to get offline monthly names.

This is what you get if you use getDisplayName from Calendar or DateFormatSymbols for January:

January (which is true for the full date string: " January 10, 2014 ")

but in the case of a standalone month name, you expect:

January

+25
Aug 15 '14 at 15:43
source share

Simpler than this

 mCalendar = Calendar.getInstance(); String month = mCalendar.getDisplayName(Calendar.MONTH, Calendar.LONG, Locale.getDefault()); 
+11
Apr 24 '14 at 10:42 on
source share

I keep this answer, which is useful for other cases, but @trutheality's answer seems to be the easiest and most direct way.

You can use DateFormatSymbols

 DateFormatSymbols(Locale.FRENCH).getMonths()[month]; // FRENCH as an example 
+7
May 31 '11 at 19:57
source share

Go into API problems, so I just made my own:

 public static String getDate(){ Time today = new Time(Time.getCurrentTimezone()); today.setToNow(); System.out.println(today.month); return today.monthDay+", "+ getMonthName(today.month) +" "+today.year; } public static String getMonthName(int month){ switch(month+1){ case 1: return "Jan"; case 2: return "Feb"; case 3: return "Mar"; case 4: return "Apr"; case 5: return "May"; case 6: return "Jun"; case 7: return "Jul"; case 8: return "Aug"; case 9: return "Sep"; case 10: return "Oct"; case 11: return "Nov"; case 12: return "Dec"; } return ""; } 
+3
Apr 15 '15 at 2:46
source share

The only way on Android is to get the properly formatted name of the month stanalone for languages ​​like ukrainian, russian, czech

 private String getMonthName(Calendar calendar, boolean short) { int flags = DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_NO_MONTH_DAY | DateUtils.FORMAT_NO_YEAR; if (short) { flags |= DateUtils.FORMAT_ABBREV_MONTH; } return DateUtils.formatDateTime(getContext(), calendar.getTimeInMillis(), flags); } 

Tested by API 15-25

Exit for May - May , but not May

+1
Apr 26 '17 at 14:20
source share

I would recommend using the Calendar and Locale object, as the names of the months are different for different languages:

 // index can be 0 - 11 private String getMonthName(final int index, final Locale locale, final boolean shortName) { String format = "%tB"; if (shortName) format = "%tb"; Calendar calendar = Calendar.getInstance(locale); calendar.set(Calendar.MONTH, index); calendar.set(Calendar.DAY_OF_MONTH, 1); return String.format(locale, format, calendar); } 

Example for the full name of the month:

 System.out.println(getMonthName(0, Locale.US, false)); 

Result: January

Example for a short month name:

 System.out.println(getMonthName(0, Locale.US, true)); 

Result: Jan

+1
May 31 '17 at 12:52
source share

Getting the stand-alone name of the month is surprisingly difficult to fulfill β€œright” in Java. (At least at the time of this writing. I am currently using Java 8).

The problem is that in some languages, including Russian and Czech, the offline version of the month is different from the "formatting" version. Also, it looks like no Java API will just give you the "best" string. Most of the answers posted here so far only offer a formatting version. The box below is a working solution for getting an offline version of one month or getting an array with all of them.

Hope this helps someone else for a while!

 /** * getStandaloneMonthName, This returns a standalone month name for the specified month, in the * specified locale. In some languages, including Russian and Czech, the standalone version of * the month name is different from the version of the month name you would use as part of a * full date. (Different from the formatting version). * * This tries to get the standalone version first. If no mapping is found for a standalone * version (Presumably because the supplied language has no standalone version), then this will * return the formatting version of the month name. */ private static String getStandaloneMonthName(Month month, Locale locale, boolean capitalize) { // Attempt to get the standalone version of the month name. String monthName = month.getDisplayName(TextStyle.FULL_STANDALONE, locale); String monthNumber = "" + month.getValue(); // If no mapping was found, then get the formatting version of the month name. if (monthName.equals(monthNumber)) { DateFormatSymbols dateSymbols = DateFormatSymbols.getInstance(locale); monthName = dateSymbols.getMonths()[month.getValue()]; } // If needed, capitalize the month name. if ((capitalize) && (monthName != null) && (monthName.length() > 0)) { monthName = monthName.substring(0, 1).toUpperCase(locale) + monthName.substring(1); } return monthName; } /** * getStandaloneMonthNames, This returns an array with the standalone version of the full month * names. */ private static String[] getStandaloneMonthNames(Locale locale, boolean capitalize) { Month[] monthEnums = Month.values(); ArrayList<String> monthNamesArrayList = new ArrayList<>(); for (Month monthEnum : monthEnums) { monthNamesArrayList.add(getStandaloneMonthName(monthEnum, locale, capitalize)); } // Convert the arraylist to a string array, and return the array. String[] monthNames = monthNamesArrayList.toArray(new String[]{}); return monthNames; } 
0
Feb 27 '16 at 1:13
source share



All Articles