尝试使用DateTimeFormatter解析时间

时间:2015-08-26 01:21:51

标签: java parsing localtime

无法解析一天中的某个时间'使用DateTimeFormatter获取LocalTime变量。尝试了几种不同的格式,但没有看到我错过的东西(正在思考,因为我使用DateTimeFormatter我需要有一个日期组件,但是我只使用LocalDate没问题,文档说它支持LocalTime )。

提前感谢任何帮助/链接,以确定导致异常的错误。

与此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());           
            }
        });
    }
}

...我得到了这个堆栈跟踪:

  

线程中的异常" AWT-EventQueue-0" java.time.format.DateTimeParseException:   文字'上午12点'无法在索引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)       在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)       在java.awt.EventDispatchThread.run(EventDispatchThread.java:82)

1 个答案:

答案 0 :(得分:4)

您应该将其转换为大写而不是小写,因为from django.db.models import Func, F Book.objects.all().annotate(title_lower=Func(F('title'), function='LOWER')).order_by('-title_lower') 只会解析大写DateTimeFormatter / AM

PM