Failed to parse single digit hour and lowercase am-pm of day into Java 8 LocalTime

故事扮演 提交于 2019-11-30 21:08:36

The AM/PM tokens have* to be uppercase:

LocalTime test = LocalTime.parse("8:00AM", DateTimeFormatter.ofPattern("hh:mma"));

Patterns definitions for hours:

Symbol  Meaning                     Presentation      Examples
------  ------------                ------------      --------
a       am-pm-of-day                text              PM
h       clock-hour-of-am-pm (1-12)  number            12
K       hour-of-am-pm (0-11)        number            0
k       clock-hour-of-am-pm (1-24)  number            0
H       hour-of-day (0-23)          number            0

Refer to the DateTimeFormatter javadocs for all available patterns: https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html

-- EDIT

*As per @Hugo's comment, this is related to the Locale your formatter is setup on. If you are not specifying any, then the JVMs default is used. It just happens that the vast majority of Locales enforces AM/PM tokens to be upper case:

It so happens that, for most locales, the uppercase AM is used, so it'll work for most people. But it can also be a.m. in ga_IE and es_US locales, 午前 in Japanese (ja_JP), fm in Swedish (sv_SE), and many others. I've tested in JDK 1.8.0_144 and it has 160 locales - 108 of them use AM - anyway, it's not an issue if your JVM uses one of those 108 locales (and nobody changes this config)

AM/PM pattern is locale sensitive. If you create a formatter and don't set a java.util.Locale, it'll use the JVM's default. Anyway, I've checked in JDK 1.8.0_144 and there's no locale that uses lowercase am as the text for AM/PM field (I've found locales that use a.m. and AM, but no am).

So, one alternative is to set a locale that uses AM (example: Locale.ENGLISH) and use a java.time.format.DateTimeFormatterBuilder to build a case insensitive formatter. Another detail is that the hour in the input has only 1 digit, so you must change the pattern to h (which accepts 1 or 2 digits, while hh accepts only 2):

DateTimeFormatter fmt = new DateTimeFormatterBuilder()
    // case insensitive
    .parseCaseInsensitive()
    // pattern
    .appendPattern("h:mma")
    // set Locale that uses "AM" and "PM"
    .toFormatter(Locale.ENGLISH);
// now it works
LocalTime test = LocalTime.parse("8:00am", fmt);

The problem is that the locale can also affect other fields (if you use month or day of week names, or week based fields, for example).

Another detail is that the formatter is case insensitive only for parsing. When formatting, it'll use the locale specific symbols, which in this case is uppercase. So this:

System.out.println(fmt.format(test)); // 8:00AM

Prints:

8:00AM


To not depend on the locale, you can use a map of custom texts for this field, using a java.time.temporal.ChronoField:

// map of custom text for AM/PM field
Map<Long, String> map = new HashMap<>();
// AM's value is 0
map.put(0L, "am");
// PM's value is 1
map.put(1L, "pm");
DateTimeFormatter fmt = new DateTimeFormatterBuilder()
    // pattern (hour:minute)
    .appendPattern("h:mm")
    // use custom text for AM/PM
    .appendText(ChronoField.AMPM_OF_DAY, map)
    // create formatter, no need to set locale
    .toFormatter();
// it also works
LocalTime test = LocalTime.parse("8:00am", fmt);

The difference from the previous formatter is that it uses the custom text (lowercase am and pm) for both parsing and formatting. So this code:

System.out.println(fmt.format(test)); // 8:00am

Will print:

8:00am

AM/PM are uppercase, and you need to use h to specify one digit for hours (with hh two digits are required). Like,

LocalTime test = LocalTime.parse("8:00am".toUpperCase(), 
        DateTimeFormatter.ofPattern("h:mma"));

You need to capitalize to AM and add a leading zero in front of the 8.

LocalTime lt=LocalTime.parse("08:00AM",DateTimeFormatter.ofPattern("hh:mma"));

Just a heads up, In Adelaide the requirement is for it to use am insteam of AM, so I am having to .toLowerCase() my dates prior to parsing from text. So "AM" fails, "am" works.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!