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?
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.
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.
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()));
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; }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With