What is the use of the toEpochSecond() method in Java 9?

JavaObject Oriented ProgrammingProgramming

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
raja
Published on 06-Mar-2020 12:53:43
Advertisements