How to format YearMonth and MonthDay depending on a Locale?

后端 未结 3 662
无人共我
无人共我 2020-12-03 19:06

Formatting a LocalDate in Java 8 using a specific Locale can be achieved like this:

DateTimeFormatter.ofLocalizedDate(FormatStyle.S         


        
3条回答
  •  遥遥无期
    2020-12-03 19:24

    You need to use DateTimeFormatter#ofPattern

    For YearMonth

    YearMonth source = YearMonth.now();
    DateTimeFormatter english = DateTimeFormatter.ofPattern("MMMM, yyyy", Locale.ENGLISH);
    DateTimeFormatter german = DateTimeFormatter.ofPattern("MMMM yyyy", Locale.GERMAN);
    
    System.out.println(source.format(english));
    System.out.println(source.format(german));
    

    For MonthDay

    MonthDay source = MonthDay.now();
    DateTimeFormatter english = DateTimeFormatter.ofPattern("MMMM dd", Locale.ENGLISH);
    DateTimeFormatter german = DateTimeFormatter.ofPattern("dd. MMMM", Locale.GERMAN);
    
    System.out.println(source.format(english));
    System.out.println(source.format(german));
    

提交回复
热议问题