Use of the toEpochSecond Method in Java 9



In Java 9, the LocalDate class provides the toEpochSecond() method to convert local date into epoch seconds. The toEpochSecond() method converts the LocalDate to a number of seconds since the epoch 1970-01-01T00:00:00Z. The LocalDate can be combined with a given time and zone offset to calculate seconds starting from 1970-01-01T00:00:00Z.

Syntax

public long toEpochSecond(LocalTime time, ZoneOffset offset)

Example

import java.time.LocalDate;
import java.time.LocalTime;
import java.time.ZoneOffset;

public class ToEpochSecondMethodTest {
   public static void main(String args[]) {
      LocalDate date = LocalDate.now();
      LocalTime time = LocalTime.now();

      System.out.println("LocalDate toEpochSecond : " + date.toEpochSecond(time, ZoneOffset.of("Z")));
      System.out.println("LocalTime toEpochSecond : " + time.toEpochSecond(date, ZoneOffset.of("Z")));
   }
}

Output

LocalDate toEpochSecond : 1583496984
LocalTime toEpochSecond : 1583496984
Updated on: 2020-03-06T08:23:43+05:30

770 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements