Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get Month Name from Calendar?

Tags:

java

calendar

Is there a oneliner to get the name of the month when we know:

int monthNumber  = calendar.get(Calendar.MONTH) 

Or what is the easiest way?

like image 350
ogzd Avatar asked Feb 12 '13 12:02

ogzd


People also ask

How do you display months in a string?

We will use it like this. SimpleDateFormat("MMM"); Let us see an example. // displaying month in MMMM format SimpleDateFormat simpleformat = new SimpleDateFormat("MMMM"); String strMonth= simpleformat.

How do I get the month name from a number in Python?

Method #1 : Using strftime() + %B In this, we use strftime() which converts date object to a string using a format, and by providing %B, it's enforced to just return a Month Name.


2 Answers

You can achieve it using SimpleDateFormat, which is meant to format date and times:

Calendar cal = Calendar.getInstance(); System.out.println(new SimpleDateFormat("MMM").format(cal.getTime())); 
like image 102
PermGenError Avatar answered Oct 24 '22 13:10

PermGenError


String getMonthForInt(int num) {     String month = "wrong";     DateFormatSymbols dfs = new DateFormatSymbols();     String[] months = dfs.getMonths();     if (num >= 0 && num <= 11) {         month = months[num];     }     return month; } 
like image 45
subodh Avatar answered Oct 24 '22 13:10

subodh