Reputation: 634
I get an integer and I need to convert to a month names in current locales:
Example for locale en-us: 1 -> January 2 -> February
sample example of my code in gremlin :
.....groupBy{[it[3], it[2].getMonth()]}{it[1]}{it.sum()}.cap().scatter().transform{[Name:it.key,Tx:it.value]}
here I get as :
==>{Name=[ABC, 7], Tx=1750.0}
i want the month integer in name format .
I tried ,
it[2].getMonth().toString() and also as cal.get(Calendar.MONTH) etc
. But no suceess.
Upvotes: 4
Views: 6100
Reputation: 416
Consider these three ways of doing that, I hope these will help you.
1)you can do as below by giving month as integer
import java.text.DateFormatSymbols;
public String getMonth(int month) {
return new DateFormatSymbols().getMonths()[month-1];
}
2)you can do as below by giving date
SimpleDateFormat dateFormat = new SimpleDateFormat( "LLLL", Locale.getDefault() );
dateFormat.format( date );
3)Using SimpleDateFormat.
import java.text.SimpleDateFormat;
public String formatMonth(String month) {
SimpleDateFormat monthParse = new SimpleDateFormat("MM");
SimpleDateFormat monthDisplay = new SimpleDateFormat("MMMM");
return monthDisplay.format(monthParse.parse(month));
}
formatMonth("2"); //Result: February
Upvotes: 1
Reputation: 171154
To convert a month integer to a String, you can do:
String month = new java.text.DateFormatSymbols().months[ 4 ]
assert month == 'May'
Upvotes: 4