JDK8: unable to parse LocalTime

后端 未结 4 1295
北荒
北荒 2020-12-17 10:16

I managed to parse a String to a LocalDate object:

DateTimeFormatter f1=DateTimeFormatter.ofPattern(\"dd MM yyyy\");
LocalDate d=Lo         


        
相关标签:
4条回答
  • 2020-12-17 10:28

    If you use a specific format, according to API:

    The string must represent a valid time and is parsed using DateTimeFormatter.ISO_LOCAL_TIME.

    hh mm 
    

    for 24h must be

    HH mm
    

    or for 12h

    kk mm
    

    The handled formats must have this conditions:

    • Two digits for the hour-of-day. This is pre-padded by zero to ensure two digits.
    • A colon
    • Two digits for the minute-of-hour. This is pre-padded by zero to ensure two digits.
    • If the second-of-minute is not available then the format is complete.
    • A colon
    • Two digits for the second-of-minute. This is pre-padded by zero to ensure two digits.
    • If the nano-of-second is zero or not available then the format is complete.
    • A decimal point
    • One to nine digits for the nano-of-second. As many digits will be output as required.
    0 讨论(0)
  • 2020-12-17 10:33

    Use DateTimeFormatter.ofPattern("kk mm"); for 12 hour clock or DateTimeFormatter.ofPattern("HH mm") for 24 hour clock

    If you want to parse time with hh you must combine it wih a where you define AM or PM:

    DateTimeFormatter f2 = DateTimeFormatter.ofPattern("hh mm a");
    LocalTime t = LocalTime.parse("11 08 AM", f2);
    
    0 讨论(0)
  • 2020-12-17 10:51

    You need to use capital HH in the pattern

    DateTimeFormatter f2=DateTimeFormatter.ofPattern("HH mm");
    

    or do this, for clock-hour-of-am-pm you need to specify it.

    This should also work

    DateTimeFormatter f2=DateTimeFormatter.ofPattern("hh mm a");
    LocalTime t=LocalTime.parse("11 08 AM",f2); //exception here
    
    0 讨论(0)
  • 2020-12-17 10:54

    In this case Unable to obtain LocalTime from TemporalAccessor means it cannot determine the how far through the day the given string represents i.e. there is not enough information to construct a LocalTime. Behind the scenes, the code looks something like this expanded Java 8 version (which gives a similar error):

    DateTimeFormatter f2 = DateTimeFormatter.ofPattern("hh mm");
    TemporalAccessor temporalAccessor = f2.parse("11 08");
    LocalTime t = temporalAccessor.query(LocalTime::from);
    System.out.println(t);
    

    The LocalTime::from documentation says

    The conversion uses the TemporalQueries.localTime() query, which relies on extracting the NANO_OF_DAY field.

    Your error is telling you that the TemporalAccessor only has two fields, neither of which is a NANO_OF_DAY field. The minimum allowable patterns for retrieving a LocalTime using a DateTimeFormatter are:

    DateTimeFormatter.ofPattern("ha");
    DateTimeFormatter.ofPattern("Ka");
    DateTimeFormatter.ofPattern("ah");
    DateTimeFormatter.ofPattern("aK");
    DateTimeFormatter.ofPattern("k");
    DateTimeFormatter.ofPattern("H");
    

    Your pattern must contain at least one of those strings to get a NANO_OF_DAY field in the internal TemporalAccessor from which a LocalTime can be constructed.

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