Skip to content

Instantly share code, notes, and snippets.

@manuelGitHub1
Created November 9, 2022 09:23
Show Gist options
  • Save manuelGitHub1/4f56403c04e867639d5e4bbce7cc817b to your computer and use it in GitHub Desktop.
Save manuelGitHub1/4f56403c04e867639d5e4bbce7cc817b to your computer and use it in GitHub Desktop.
Localize month name
public static String getGermanMonthName( final String monthName ) {
return getLocalizedMonthName(monthName, Locale.GERMAN);
}
public static String getLocalizedMonthName( final String monthName, final Locale locale ) {
if ( monthName == null || monthName.isBlank() ) {
return null;
}
try {
final Month month = Month.valueOf(monthName.toUpperCase());
final MonthDay monthDay = MonthDay.of(month, 1);
final DateTimeFormatter german = DateTimeFormatter.ofPattern("MMMM", locale);
return monthDay.format(german);
}
catch ( IllegalArgumentException e ) {
System.err.println(monthName + " is not a valid month");
}
return null;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment