我设法解析String一个LocalDate对象:
DateTimeFormatter f1=DateTimeFormatter.ofPattern("dd MM yyyy");
LocalDate d=LocalDate.parse("26 08 1984",f1);
System.out.println(d); //prints "1984-08-26"
Run Code Online (Sandbox Code Playgroud)
但我不能这样做LocalTime.这段代码:
DateTimeFormatter f2=DateTimeFormatter.ofPattern("hh mm");
LocalTime t=LocalTime.parse("11 08",f2); //exception here
System.out.println(t);
Run Code Online (Sandbox Code Playgroud)
抛出一个DateTimeParseException:
Exception in thread "main" java.time.format.DateTimeParseException: Text '11 08' could not be parsed: Unable to obtain LocalTime from TemporalAccessor: {MinuteOfHour=8, HourOfAmPm=11},ISO of type java.time.format.Parsed
at java.time.format.DateTimeFormatter.createError(Unknown Source)
at java.time.format.DateTimeFormatter.parse(Unknown Source)
at java.time.LocalTime.parse(Unknown Source)
at com.mui.cert.Main.<init>(Main.java:21)
at com.mui.cert.Main.main(Main.java:12)
Caused by: java.time.DateTimeException: Unable to obtain LocalTime from TemporalAccessor: {MinuteOfHour=8, HourOfAmPm=11},ISO of type …Run Code Online (Sandbox Code Playgroud) LocalDateTime.parse("2017-02-02 08:59:12", DateTimeFormatter.ofPattern("yyyy-MM-dd hh:mm:ss"));
Run Code Online (Sandbox Code Playgroud)
它打印错误:
java.time.format.DateTimeParseException: Text '2017-02-02 08:59:12' could not be parsed: Unable to obtain LocalDateTime from TemporalAccessor: {MinuteOfHour=59, NanoOfSecond=0, SecondOfMinute=12, MicroOfSecond=0, MilliOfSecond=0, HourOfAmPm=8},ISO resolved to 2017-02-02 of type java.time.format.Parsed
Run Code Online (Sandbox Code Playgroud)
Accoeding消息看起来像所有值解析正确,但无论如何我看到错误.
如何让它工作?
我有以下字符串:
18/07/2019 16:20
我尝试LocalDateTime使用以下代码将此字符串转换为:
val stringDate = expiration_button.text.toString()
val date = LocalDateTime.parse(stringDate, DateTimeFormatter.ofPattern("dd/MM/yyyy hh:mm")).toString()
Run Code Online (Sandbox Code Playgroud)
java.time.format.DateTimeParseException:无法解析文本“18/07/2019 04:30:00”:无法从 TemporalAccessor 获取 LocalDateTime
我缺少什么?