Win a copy of Svelte and Sapper in Action this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

DateTimeFormatter question

 
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;"
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • 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, ;-)
 
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


 
Marshal
Posts: 70260
282
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
...and welcome to the Ranch
 
moose poop looks like football shaped elk poop. About the size of this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic