Java Program to get the seconds since the beginning of the Java epoch

Java 8Object Oriented ProgrammingProgramming

To get the seconds since the beginning of epochs, you need to use Instant. The method here used is ofEpochSecond() method. The epoch is the number of seconds that have elapsed since 00::00:00 Thursday, 1 January 1970.

Get the seconds with ChronoUnit.SECONDS −

long seconds = Instant.ofEpochSecond(0L).until(Instant.now(), ChronoUnit.SECONDS);

Example

import java.time.Instant;
import java.time.temporal.ChronoUnit;
public class Demo {
   public static void main(String[] args) {
      long seconds = Instant.ofEpochSecond(0L).until(Instant.now(), ChronoUnit.SECONDS);
      System.out.println("Seconds since the beginning of the Java epoch = "+seconds);
   }
}

Output

Seconds since the beginning of the Java epoch = 1555053202
raja
Published on 26-Apr-2019 10:02:37
Advertisements