Get the month (or day) name (localized)Tag(s): Date and Time Internationalization


import java.text.DateFormatSymbols;
import java.util.Locale;

public class DateUtils {
  private DateUtils() {  }

  public static String getMonthName(int month) {
    return getMonthName(month, Locale.getDefault());
  }

  public static String getMonthName(int month, Locale locale) {
    DateFormatSymbols symbols = new DateFormatSymbols(locale);
    String[] monthNames = symbols.getMonths();
    return monthNames[month - 1];
  }


  public static String getDayName(int day, Locale locale) {
    DateFormatSymbols symbols = new DateFormatSymbols(locale);
    String[] dayNames = symbols.getWeekdays();
    return dayNames[day];
  }


  public static void main(String[] args) {

    System.out.println(DateUtils.getMonthName(1));
    System.out.println(DateUtils.getMonthName(1, new Locale("it")));

    System.out.println
       (DateUtils.getDayName(java.util.Calendar.SUNDAY, Locale.getDefault()));

    /*
     * output :
     *   january
     *   gennaio
     *   sunday
     */
  }
}

blog comments powered by Disqus