Syntax error Display seconds since the epoch with Java Date and Time Conversion Character

Display seconds since the epoch with Java Date and Time Conversion Character



To display seconds since the epoch, use the ‘s’ Date and Time conversion specifier.

System.out.printf("Seconds since epoch = %ts", d);

The above would display seconds since.

1970-01-01 00:00:00 GMT

Example

 Live Demo

import java.util.Date;
public class Demo {
   public static void main(String[] args) {
      Date d = new Date();
      System.out.printf("Nanoseconds = %tN\n", d);
      System.out.printf("Seconds since epoch = %ts", d);
   }
}

Output

Nanoseconds = 364000000
Seconds since epoch = 1543241402
Updated on: 2020-06-27T05:38:01+05:30

447 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements