• Post Reply Bookmark Topic Watch Topic
  • New Topic

Time API DateTimeFormatter Exception with LocalDateTime  RSS feed

 
Ashley Bye
Ranch Hand
Posts: 140
2
Java Mac
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The following throws an unchecked exception (shown after) on the formatter assignment line:

Exception:

However, the following two variants execute without problem:


Even using FormatStyle.FULL causes the same exception although the API documentation clearly shows a time as part of this output. Although LONG does not show this, I would still expect it to provide the time as part of the format. Nowhere in the documentation does it say that LONG and FULL cannot be used with a time.

Has anyone else had this problem? What causes it? Is there a fix, other than to use the 2 argument method? Or is it a bug, and if so, does Oracle have a bug reporting system?
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37181
515
Eclipse IDE Java VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ashley,
Sorry it took you a week to get a reply. This was an interesting/tough problem. I think I understand it. This code works:


On my machine, in prints out:
now: Wednesday, June 3, 2015 9:41:22 PM EDT

And if I change it to long, it prints out:
now: June 3, 2015 9:43:18 PM EDT

The only difference I see between the two is that FULL includes the day of the week. Both include the time zone. This explains why they failed in your example. LocalDateTime doesn't have a time zone so the formatter didn't know what to print for the time zone field.

It's hard to say whether it is a bug or just misleading documentation. The JavaDoc for LONG says "Long text style, with lots of detail. For example, the format might be 'January 12, 1952'." It says "might be" so technically it is an example. You can report a bug here and see what they say. I think it is a documentation flaw rather than a code flaw. Hopefully your bug report inspires them to fix the docs in a future release.

For FULL, it says "For example, the format might be 'Tuesday, April 12, 1952 AD' or '3:30:42pm PST'.". For that one, clearly says it includes a time zone at least.
 
Ashley Bye
Ranch Hand
Posts: 140
2
Java Mac
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for taking the time to reply Jeanne. I have also managed to get it to work using a ZonedDateTime. I suspect this might be why there is a second static ofLocalizedDateTime() method that takes 2 FormatStyle parameters for the LocalDateTime formatter and that the documentation is just a little ambiguous. I have submitted a bug report with respect to the documentation and will post here once I have received a response.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!