0

I am getting error ofInstant not member of LocalDate in jdk 8.

LocalDate.ofInstant(Instant.now(), zone).atStartOfDay(ZoneId.of("Z")).toEpochSecond

It's running fine in jdk11. How can the same be done in most decent manner in jdk8.

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
ARYA
  • 223
  • 1
  • 5
  • 15
  • 1
    A similar problem which will solve Instant to Localdate conversion https://stackoverflow.com/questions/52264768/how-to-convert-from-instant-to-localdate – Eklavya Jun 08 '20 at 17:47

1 Answers1

0

The LocalDate.ofInstant method was introduced in Java 9. Here’s the version I would use no matter if using Java 8, 9 or 11:

    ZoneId zone = ZoneId.of("Asia/Gaza");
    long epochSecond = LocalDate.now(zone).atStartOfDay(ZoneOffset.UTC).toEpochSecond();
    System.out.println("epochSecond: " + epochSecond);

It gives the same result as your code if you fill in the same time zone.

If you need to convert from a given Instant, in Java 8 do:

    Instant inst = Instant.now(); // Assign your Instant value here
    long epochSecond = inst.atZone(zone)
            .toLocalDate()
            .atStartOfDay(ZoneOffset.UTC)
            .toEpochSecond();
Ole V.V.
  • 81,772
  • 15
  • 137
  • 161