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.
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.
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();