DateTimeFormatter month pattern letter "L" fails
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.)
“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.
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
.