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


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

Updated on: 30-Jul-2019

386 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements