1337
1337

Reputation: 23

cannot resolve symbol ofPattern

I'm trying to use Java time to format a date in a particular way so that I can search for the date in a text file. I've imported java.time.format.DateTimeFormatter but when I enter the code

DateTimeFormatter formatDate = new DateTimeFormatter.ofPattern("dd/MM/yyyy");

it displays the error "Cannot resolve symbol ofPattern". I can't see what I'm doing wrong so any help would be greatly appreciated! Thanks

Upvotes: 2

Views: 2668

Answers (1)

Sergey Prokofiev
Sergey Prokofiev

Reputation: 1885

There are two issues in this snippet:

  1. As Phil correctly pointed, there is no method ofPattern taking zero arguments. According javadoc you should use either ofPattern(String pattern), either ofPattern(String pattern, Locale locale)
  2. Both such methods are static, so you don't need object at all

Bringing all the things together

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy MM dd");

Hope it helps!

Upvotes: 7

Related Questions