Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I convert an Integer to localized month name in Java?

Tags:

java

date

locale

I get an integer and I need to convert to a month names in various locales:

Example for locale en-us:
1 -> January
2 -> February

Example for locale es-mx:
1 -> Enero
2 -> Febrero

like image 450
atomsfat Avatar asked Jun 24 '09 14:06

atomsfat


People also ask

How do you convert int to month?

DateTime dt = DateTime. Now; Console. WriteLine( dt. ToString( "MMMM" ) );


2 Answers

import java.text.DateFormatSymbols; public String getMonth(int month) {     return new DateFormatSymbols().getMonths()[month-1]; } 
like image 148
joe Avatar answered Sep 29 '22 23:09

joe


You need to use LLLL for stand-alone month names. this is documented in the SimpleDateFormat documentation, such as:

SimpleDateFormat dateFormat = new SimpleDateFormat( "LLLL", Locale.getDefault() ); dateFormat.format( date ); 
like image 23
Ilya Lisway Avatar answered Sep 29 '22 22:09

Ilya Lisway