1
votes

Having trouble parsing only a 'time of day' for a LocalTime variable using DateTimeFormatter. Tried several different formats, but not seeing what I'm missing (was thinking since I'm using DateTimeFormatter I need to have a date component, but I use it with just LocalDate no problem and the documentation says it supports LocalTime).

Thanks in advance for any help/links identifying my mistake that causes the exception.

Replicated with this SSCCE:

import java.awt.*;
import java.time.*;
import java.time.format.*;

public class SSCCE {
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable(){          
            @Override
            public void run() {
                String word = "";

                word = "1200am";
//              System.out.println(LocalTime.parse(word.toLowerCase(),  DateTimeFormatter.ISO_LOCAL_TIME).toString());
                System.out.println(LocalTime.parse(word.toLowerCase(), DateTimeFormatter.ofPattern("hhmma")).toString());
//              System.out.println(LocalTime.parse(word.toLowerCase(), DateTimeFormatter.ofPattern("hmma")).toString());
//              System.out.println(LocalTime.parse(word.toLowerCase(), DateTimeFormatter.ofPattern("HHmma")).toString());
//              System.out.println(LocalTime.parse(word.toLowerCase(), DateTimeFormatter.ofPattern("Hmma")).toString());
//              System.out.println(LocalTime.parse((CharSequence)word.toLowerCase(), DateTimeFormatter.ofPattern("hhma")).toString());

            word = "8PM";
            System.out.println(LocalTime.parse(word.toLowerCase(), DateTimeFormatter.ISO_LOCAL_TIME).toString());           
            }
        });
    }
}

...and I get this stack trace:

Exception in thread "AWT-EventQueue-0" java.time.format.DateTimeParseException: Text '1200am' could not be parsed at index 4 at java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:194 7) at java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1849) at java.time.LocalTime.parse(LocalTime.java:441) at SSCCE$1.run(SSCCE.java:14) at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:311) at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:756) at java.awt.EventQueue.access$500(EventQueue.java:97) at java.awt.EventQueue$3.run(EventQueue.java:709) at java.awt.EventQueue$3.run(EventQueue.java:703) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.ja va:75) at java.awt.EventQueue.dispatchEvent(EventQueue.java:726) at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java :201) at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:11 6) at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java :105) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93) at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)

1

1 Answers

4
votes

You should convert it to uppercase instead of lowercase because the DateTimeFormatter will only parse uppercase AM/PM:

System.out.println(LocalTime.parse(word.toUpperCase(),
        DateTimeFormatter.ofPattern("hhmma")).toString());

// will print 00:00