Java Data Type How to - Get How many seconds have occurred since the beginning of the Java epoch








Question

We would like to know how to get How many seconds have occurred since the beginning of the Java epoch.

Answer

import java.time.Instant;
import java.time.temporal.ChronoUnit;
//w ww.  j av  a  2 s .c o m
public class Main {

  public static void main(String[] args) {
    // Instant is useful for generating a time stamp to represent machine time.
    Instant timestamp = Instant.now();

    // How many seconds have occurred since the beginning of the Java epoch.
    long secondsFromEpoch = Instant.ofEpochSecond(0L).until(Instant.now(), ChronoUnit.SECONDS);
    
    System.out.println(secondsFromEpoch);
  }
}

The code above generates the following result.