DateTimeFormatter month pattern letter “L” fails

前端 未结 3 1044
南方客
南方客 2020-11-30 11:13

I noticed that java.time.format.DateTimeFormatter is not able to parse out as expected. See below:

import java.time.L         


        
3条回答
  •  情话喂你
    2020-11-30 11:36

    “stand-alone” month name

    I believe 'L' is meant for languages that use a different word for the month itself versus the way it is used in a date. For example:

    Locale russian = Locale.forLanguageTag("ru");
    
    asList("MMMM", "LLLL").forEach(ptrn -> 
        System.out.println(ptrn + ": " + ofPattern(ptrn, russian).format(Month.MARCH))
    );
    

    Output:

    MMMM: марта
    LLLL: Март
    

    There shouldn't be any reason to use 'L' instead of 'M' when parsing a date.

    I tried the following to see which locales support stand-alone month name formatting:

    Arrays.stream(Locale.getAvailableLocales())
        .collect(partitioningBy(
                    loc -> "3".equals(Month.MARCH.getDisplayName(FULL_STANDALONE, loc)),
                    mapping(Locale::getDisplayLanguage, toCollection(TreeSet::new))
        )).entrySet().forEach(System.out::println);
    

    The following languages get a locale-specific stand-alone month name from 'LLLL':

    Catalan, Chinese, Croatian, Czech, Finnish, Greek, Hungarian, Italian, Lithuanian, Norwegian, Polish, Romanian, Russian, Slovak, Turkish, Ukrainian

    All other languages get "3" as a stand-alone name for March.

提交回复
热议问题