Home  >  Article  >  Java  >  What is the purpose of toEpochSecond() method in Java 9?

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

王林
王林forward
2023-09-04 18:09:021025browse

在Java 9中,toEpochSecond()方法的用途是什么?

In Java 9, the LocalDate class provides the toEpochSecond() method for converting local date to epochsecond. The toEpochSecond() method converts LocalDate to the number of seconds since epoch 1970-01-01T00:00:00Z. LocalDate can be used in conjunction with a given time and time zone offset to calculate the number of seconds since 1970-01-01T00:00:00Z. The Chinese translation of

Grammar

<strong>public long toEpochSecond(LocalTime time, ZoneOffset offset)</strong>

Example

is:

Example

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

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

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

Output

<strong>LocalDate toEpochSecond : 1583496984
LocalTime toEpochSecond : 1583496984</strong>

The above is the detailed content of What is the purpose of toEpochSecond() method in Java 9?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete