Re: [threeten-develop] LocalDateTime fails to parse date-only pattern
Status: Alpha
Brought to you by:
scolebourne
From: Roger R. <Roger.Riggs@Oracle.com> - 2014-10-08 04:37:50
|
Hi Ian, I think you answered your own question. If the pattern and the input provide only date information then only a LocalDate can be parsed. The nested exception says "Unable to obtain LocalTime from TemporalAccessor" that indicates there is no time information in the input and so a LocalTime cannot be created. Parsing can ignore extra information from the input but cannot fabricate fields from no data. Regards, Roger On 10/7/14 6:56 PM, Ian Brandt wrote: > Greetings, > > Feel free to direct me to another venue if this is the wrong place to post this. I don’t have author status on the JDK Bug System, so I can’t file an issue there. > > The new java.time API allows for the creation of a DateTimeFormatter pattern that only includes a date portion; however, LocalDateTime fails to parse such a pattern with a rather cryptic error: > > LocalDateTimeParseDateTest.java > ——— > import java.time.*; > import java.time.format.*; > import java.util.*; > > public class LocalDateTimeParseDateTest { > public static void main(String[] args) { > String dateString = "24-Aug-2009"; > DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("dd-MMM-yyyy", Locale.US); > LocalDateTime.parse(dateString, dateTimeFormatter); > } > } > ——— > > $ java -version > java version "1.8.0_20" > Java(TM) SE Runtime Environment (build 1.8.0_20-b26) > Java HotSpot(TM) 64-Bit Server VM (build 25.20-b23, mixed mode) > > $ java LocalDateTimeParseDateTest > Exception in thread "main" java.time.format.DateTimeParseException: Text '24-Aug-2009' could not be parsed: Unable to obtain LocalDateTime from TemporalAccessor: {},ISO resolved to 2009-08-24 of type java.time.format.Parsed > at java.time.format.DateTimeFormatter.createError(DateTimeFormatter.java:1918) > at java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1853) > at java.time.LocalDateTime.parse(LocalDateTime.java:492) > at LocalDateTimeParseDateTest.main(LocalDateTimeParseDateTest.java:9) > Caused by: java.time.DateTimeException: Unable to obtain LocalDateTime from TemporalAccessor: {},ISO resolved to 2009-08-24 of type java.time.format.Parsed > at java.time.LocalDateTime.from(LocalDateTime.java:461) > at java.time.LocalDateTime$$Lambda$7/245257410.queryFrom(Unknown Source) > at java.time.format.Parsed.query(Parsed.java:226) > at java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1849) > ... 2 more > Caused by: java.time.DateTimeException: Unable to obtain LocalTime from TemporalAccessor: {},ISO resolved to 2009-08-24 of type java.time.format.Parsed > at java.time.LocalTime.from(LocalTime.java:409) > at java.time.LocalDateTime.from(LocalDateTime.java:457) > ... 5 more > > > It works fine with “LocalDate.parse(dateString, dateTimeFormatter)”. I also noticed it works for LocalDate with a pattern and input string that include a time portion. To summarize: > > - LocalDate can use a DateTimeFormatter to parse even with a pattern that includes a time portion. > - LocalDateTime cannot use a DateTimeFormatter to parse unless the pattern includes a time portion. > > So a “narrowing conversion” from date-time to date works, but a “widening conversion” from date to date-time does not. I’m sure it’s debatable, but that seems a bit backwards to me. I believe a Joda DateTimeFormatter with a date-only pattern successfully parses a Joda DateTime by just assuming start-of-day for the time portion. I think it would be an improvement if java.time followed suit. > > Best regards, > > Ian > ------------------------------------------------------------------------------ > Meet PCI DSS 3.0 Compliance Requirements with EventLog Analyzer > Achieve PCI DSS 3.0 Compliant Status with Out-of-the-box PCI DSS Reports > Are you Audit-Ready for PCI DSS 3.0 Compliance? Download White paper > Comply to PCI DSS 3.0 Requirement 10 and 11.5 with EventLog Analyzer > http://pubads.g.doubleclick.net/gampad/clk?id=154622311&iu=/4140/ostg.clktrk > _______________________________________________ > threeten-develop mailing list > thr...@li... > https://lists.sourceforge.net/lists/listinfo/threeten-develop |