LocalDate类的toEpochSecond()方法用于将此LocalDate转换为自1970-01-01T00:00:00Z以来的秒数。该方法将此本地日期与指定的时间和作为参数传递的偏移量相结合,以计算epoch-second值,该值是从1970-01-01T00:00:00Z开始经过的秒数。时期之后的时间轴上的瞬间为正,早期为负。
用法:
public long toEpochSecond(LocalTime time, ZoneOffset offset)
参数:此方法接受两个参数time和offset,它们是本地时间和区域偏移。
返回值:此方法返回long,它是自1970-01-01T00:00:00Z的纪元以来的秒数,可能为负数。
以下示例程序旨在说明toEpochSecond()方法:
示例1:
// Java program to demonstrate
// LocalDate.toEpochSecond() method
import java.time.*;
public class GFG {
public static void main(String[] args)
{
// create a LocalDate object
LocalDate localD
= LocalDate.parse("2018-12-06");
// print LocalDate
System.out.println("LocalDate: "
+ localD);
// create a LocalTime object
LocalTime time
= LocalTime.parse("20:12:32");
// print Instant
System.out.println("Passed LocalTime: "
+ time);
// create ZoneId
ZoneOffset zone = ZoneOffset.of("Z");
// print ZoneId
System.out.println("Passed ZoneOffset: "
+ zone);
// print result
System.out.println("Epoch Second: "
+ localD.toEpochSecond(time, zone));
}
}
输出:
LocalDate: 2018-12-06 Passed LocalTime: 20:12:32 Passed ZoneOffset: Z Epoch Second: 1544127152
示例2:
// Java program to demonstrate
// LocalDate.toEpochSecond() method
import java.time.*;
public class GFG {
public static void main(String[] args)
{
// create a LocalDate object
LocalDate localD
= LocalDate.parse("2019-01-01");
// print LocalDate
System.out.println("LocalDate: "
+ localD);
// create a LocalTime object
LocalTime time
= LocalTime.parse("00:00:00");
// print Instant
System.out.println("Passed LocalTime: "
+ time);
// create ZoneId
ZoneOffset zone = ZoneOffset.of("Z");
// print ZoneId
System.out.println("Passed ZoneOffset: "
+ zone);
// print result
System.out.println("Epoch Second: "
+ localD.toEpochSecond(time, zone));
}
}
输出:
LocalDate: 2019-01-01 Passed LocalTime: 00:00 Passed ZoneOffset: Z Epoch Second: 1546300800
相关用法
- Java ChronoZonedDateTime toEpochSecond()用法及代码示例
- Java ChronoLocalDateTime toEpochSecond()用法及代码示例
- Java LocalTime toEpochSecond()用法及代码示例
- Java LocalDate get()用法及代码示例
- Java LocalDate with()用法及代码示例
- Java LocalDate now()用法及代码示例
- Java LocalDate plus()用法及代码示例
- Java LocalDate now()用法及代码示例
- Java LocalDate getMonthValue()用法及代码示例
- Java LocalDate getYear()用法及代码示例
- Java LocalDate hashCode()用法及代码示例
- Java LocalDate isAfter()用法及代码示例
- Java LocalDate isBefore()用法及代码示例
- Java LocalDate minusDays()用法及代码示例
- Java LocalDate plusYears()用法及代码示例
注:本文由纯净天空筛选整理自AmanSingh2210大神的英文原创作品 LocalDate toEpochSecond() method in Java with Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。