public final class Instant extends Object This class models a single instantaneous point on the time-line. This might be used to record event time-stamps in the application.
public static Instant now()
Obtains the current instant from the system clock.This will query the system UTC clock to obtain the current instant.Using this method will prevent the ability to use an alternate time-source for testing because the clock is effectively hard-coded.
public long toEpochMilli()
Converts this instant to the number of milliseconds from the epoch of 1970-01-01T00:00:00Z. If this instant represents a point on the time-line too far in the future or past to fit in a long milliseconds, then an exception is thrown. If this instant has greater than millisecond precision, then the conversion will drop any excess precision information as though the amount in nanoseconds was subject to integer division by one million.
package com.candidjava.datetime; import java.time.Instant; import java.time.LocalDate; import java.time.ZoneId; public class ConvertEpochToDate { public static void main(String[] args) { long epoch=Instant.now().toEpochMilli(); System.out.println("Epoch Time:"+epoch); LocalDate date=Instant.ofEpochMilli(epoch).atZone(ZoneId.systemDefault()).toLocalDate(); System.out.println("Date:"+date); } }
OUTPUT
Epoch Time:1605708617207 Date:2020-11-18