DateTimeFormatter month pattern letter “L” fails

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

    While the other answers give excellent information on pattern letter L and date parsing, I should like to add that you should really avoid the problem altogether. Don’t get date (and time) as string from your database. Instead use an appropriate datetime object.

        String sql = "select sysdate as dt from dual;"
        PreparedStatement stmt = yourDatabaseConnection.prepareStatement(sql);
        ResultSet rs = stmt.executeQuery();
        if (rs.next()) {
            LocalDateTime dateTime = rs.getObject("dt", LocalDateTime.class);
            // do something with dateTime
        }
    

    (Not tested since I haven’t got an Oracle database at hand. Please forgive any typo.)

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题