Created
November 9, 2022 09:23
-
-
Save manuelGitHub1/4f56403c04e867639d5e4bbce7cc817b to your computer and use it in GitHub Desktop.
Localize month name
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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