• Post Reply Bookmark Topic Watch Topic
  • New Topic

DateTimeFormatter question  RSS feed

 
Arend van der Kolk
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the Study Guide "Oracle Certified Associate Java SE 8 Programmer" on page 148 they give below example.

What do i need to add/change to avoid below compile errors?

I do get the attached quick fix options (Create constant and Create field)

In my IDE i get the following compiler errors on line 14, 15 and 16
14) ISO_LOCAL_DATE cannot be resolved or is not a field
15) ISO_LOCAL_TIME cannot be resolved or is not a field
16) ISO_LOCAL_DATE_TIME cannot be resolved or is not a field

DateTimeFormatter_quick_fix_options.png
[Thumbnail for DateTimeFormatter_quick_fix_options.png]
 
Arend van der Kolk
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My guess is that somehow it does not import that class "java.time.format.DateTimeFormatter" properly.

I say that because when i hover above the word DateTimeFormatter it does not show the java library for that class. See attached.
DateTimeFormatter_class.png
[Thumbnail for DateTimeFormatter_class.png]
 
Arend van der Kolk
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My last reply was incorrect. It does show the library explanation when i add "import java.time.format.DateTimeFormatter;"
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This happens because your own class is also called DateTimeFormatter.

The Java compiler now thinks it has to look in your own class DateTimeFormatter instead of the standard class java.time.format.DateTimeFormatter.

Rename your own class to something else.

In your second example, which has class DateTimeFormatter2, it is still going wrong because you still have your other class DateTimeFormatter from the first example in the package chapter3.
 
Arend van der Kolk
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you Jesper, so simple but so far away if you don't see it, ;-)
 
Juan Rogui
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jesper de Jong wrote:This happens because your own class is also called DateTimeFormatter.

The Java compiler now thinks it has to look in your own class DateTimeFormatter instead of the standard class java.time.format.DateTimeFormatter.

Rename your own class to something else.

In your second example, which has class DateTimeFormatter2, it is still going wrong because you still have your other class DateTimeFormatter from the first example in the package chapter3.


Only to add more info. Just note that you import DateTime using wildcard and remember that Java has preference the more concrete import (not valid in this case, only remember). Anyway, you can make your code work importing these static properties and keep your class with DateTimeFormatter name


 
Campbell Ritchie
Marshal
Posts: 56593
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
...and welcome to the Ranch
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!