gpt4 book ai didi

java - 尝试使用 DateTimeFormatter 解析时间

转载 作者:行者123 更新时间:2023-11-29 03:05:51 24 4
gpt4 key购买 nike

使用 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());
}
});
}
}

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

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)

最佳答案

您应该将其转换为大写而不是小写,因为 DateTimeFormatter 只会解析大写 AM/PM:

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

// will print 00:00

关于java - 尝试使用 DateTimeFormatter 解析时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32216548/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com