Both of them don't work. I wrote the errors after "—"
LocalDateTime date = new LocalDateTime.now(); // cannot resolve symbol 'now'
LocalDateTime date = new LocalDateTime(); // LocalDateTime has private access
Both of them don't work. I wrote the errors after "—"
LocalDateTime date = new LocalDateTime.now(); // cannot resolve symbol 'now'
LocalDateTime date = new LocalDateTime(); // LocalDateTime has private access
On
Your syntax is off, assuming you're using Java 8+, without an import it might look like,
java.time.LocalDateTime date = java.time.LocalDateTime.now();
and if you have import java.time.LocalDateTime; then you only need
LocalDateTime date = LocalDateTime.now();
to invoke the static LocalDateTime#now() (note there are multiple now() functions provided, this makes it much easier to effectively use different time zones).
Your error message
LocalDateTime has private accessindicates that the compiler has importedLocalDateTimesuccessfully.First, validate that you're using the
LocalDateTimewe expect. Look at the imports. You should see:Now read the Javadoc for this class.
new LocalDateTime()is trying to invoke a zero-argument constructor. The Javadoc does not list any constructors, because there are none that are not private.new LocalDateTime.now()is trying to invoke the zero-argument constructor of a class calledLocalDateTime.now. There is no class with that name, which is why you get the errorcannot resolve symbol 'now'What you actually want to do is to call the static method
now()of theLocalDateTimeclass. To do that you do not usenew.Try creating your own class with static factory methods, and remind yourself how to call its methods.
You'll find that you get the same errors if you try to use this from another class with
new MyThing()ornew MyThing.thing().MyThing.thing()will work.