1

I have following scala code:

LocalDate.ofInstant(instant, zoneId)

instant is java.time.Instant, zoneId is java.time.ZoneId.

Intellij project is configured to jdk 17 and language level to 15.

But I get error:

value ofInstant is not a member of object java.time.LocalDate

I am unable to find why. Java versions are correct one I think.

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
Mandroid
  • 6,200
  • 12
  • 64
  • 134
  • Given that the method [exists](https://www.cs.usfca.edu/~cs272/javadoc/api/java.base/java/time/LocalDate.html#ofInstant(java.time.Instant,java.time.ZoneId)) and that you mention that is **IntelliJ** the one mentioning the error I will just assume is a bug in the IDE, can you confirm that if you try to compile using **sbt** or whatever build tool you are using the problem remains? – Luis Miguel Mejía Suárez May 23 '22 at 14:49
  • 1
    That `ofInstant` exists since java 9. Which leads me to believe you are using java 8 despite java 17. – Joop Eggen May 23 '22 at 14:52
  • I have configured project to jdk 17. – Mandroid May 23 '22 at 14:53
  • I compile with 'sbt clean compile' command. – Mandroid May 23 '22 at 14:55
  • No local variable. – Mandroid May 23 '22 at 15:08
  • Ah then maybe is the default of the `target` option? Try adding this to your `build.sbt` file : `scalacOptions ++= Seq("-release", "17", "-target:17")` _(change `17` with the minimum version of the **JRE** you want to support)_ – Luis Miguel Mejía Suárez May 23 '22 at 15:26
  • but as I have configured my intellij project to jdk 17, sbt server must target 17. – Mandroid May 23 '22 at 16:16
  • @Mandroid _"sbt server must target 17"_ yes, **sbt** will be running on top of a **Java** `17` VM, however that doesn't mean it will default to produce **Java 17** bytecode. IIRC **Scala** by default always produces **Java 8** bytecode, the flags I shared are to configure and force the compiler to produce the bytecode version you want; why don't you give it a try? – Luis Miguel Mejía Suárez May 23 '22 at 16:24
  • I actually tried it, but I still see very same issue. – Mandroid May 23 '22 at 16:28
  • @Mandroid sorry, I can't reproduce the issue, it seems that even without configuring **sbt** it is able to compile that code because my **JDK** is `openjdk version "11.0.10" 2021-01-19`, thus it seems you have some problem elsewhere. – Luis Miguel Mejía Suárez May 24 '22 at 16:50

1 Answers1

3

In Java 8 to get LocalDate from Instant you can do as follows

 LocalDate localDate = LocalDate.from(ZonedDateTime.ofInstant(Instant.now(), ZoneId.systemDefault()));
Michael Gantman
  • 7,315
  • 2
  • 19
  • 36