DateTimeFormatter month pattern letter “L” fails

前端 未结 3 1045
南方客
南方客 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

    According to the javadocs:

    Pattern letters 'L', 'c', and 'q' specify the stand-alone form of the text styles.

    However, I couldn't find much about what the "stand-alone" form is supposed to be. In looking at the code I see that using 'L' selects TextStyle.SHORT_STANDALONE and according to that javadoc:

    Short text for stand-alone use, typically an abbreviation. For example, day-of-week Monday might output "Mon".

    However, that isn't how it seems to work. Even with three letters I get numerical output from this code:

    DateTimeFormatter pattern = DateTimeFormatter.ofPattern ("dd-LLL-yyyy");
    System.out.println (pattern.format (LocalDate.now ()));
    

    Edit

    After further investigation it seems (as near as I can tell) that the "stand-alone" versions of these codes are for when you want to load your own locale-independent data, presumably using DateTimeFormatterBuilder. As such, by default DateTimeFormatter has no entries loaded for TextStyle.SHORT_STANDALONE.

提交回复
热议问题