ChronoLocalDateTime類的toInstant()方法用於將此ChronoLocalDateTime轉換為Instant。該方法將此ChronoLocalDateTime與作為參數傳遞的偏移量結合起來以計算Instant。
用法:
default Instant toInstant(ZoneOffset offset)
參數:此方法接受參數offset,即區域偏移。
返回值:此方法返回Instant,這是此ChronoLocalDateTime的Instant。
以下示例程序旨在說明toInstant()方法:
示例1:
// Java program to demonstrate
// ChronoLocalDateTime.toInstant() method
import java.time.*;
import java.time.chrono.*;
public class GFG {
public static void main(String[] args)
{
// create ChronoLocalDateTime object
ChronoLocalDateTime time
= LocalDateTime
.parse("2019-12-31T19:15:30");
// print ChronoLocalDateTime
System.out.println("ChronoLocalDateTime: "
+ time);
// create ZoneId
ZoneOffset zone = ZoneOffset.of("Z");
// print ZoneId
System.out.println("Passed ZoneOffset: "
+ zone);
// print result
System.out.println("Instant: "
+ time.toInstant(zone));
}
}
輸出:
ChronoLocalDateTime: 2019-12-31T19:15:30 Passed ZoneOffset: Z Instant: 2019-12-31T19:15:30Z
示例2:
// Java program to demonstrate
// ChronoLocalDateTime.toInstant() method
import java.time.*;
import java.time.chrono.*;
public class GFG {
public static void main(String[] args)
{
// create ChronoLocalDateTime object
ChronoLocalDateTime time
= LocalDateTime.parse(
"2018-10-25T23:12:31.123");
// print ChronoLocalDateTime
System.out.println("ChronoLocalDateTime: "
+ time);
// create ZoneId
ZoneOffset zone = ZoneOffset.of("Z");
// print ZoneId
System.out.println("Passed ZoneOffset: "
+ zone);
// print result
System.out.println("Instant: "
+ time.toInstant(zone));
}
}
輸出:
ChronoLocalDateTime: 2018-10-25T23:12:31.123 Passed ZoneOffset: Z Instant: 2018-10-25T23:12:31.123Z
相關用法
- Java ChronoZonedDateTime toInstant()用法及代碼示例
- Java Date toInstant()用法及代碼示例
- Java ChronoLocalDateTime from()用法及代碼示例
- Java ChronoLocalDateTime get()用法及代碼示例
- Java ChronoLocalDateTime until()用法及代碼示例
- Java ChronoLocalDateTime hashCode()用法及代碼示例
- Java ChronoLocalDateTime getChronology()用法及代碼示例
- Java ChronoLocalDateTime toLocalDate()用法及代碼示例
- Java ChronoLocalDateTime equals()用法及代碼示例
- Java ChronoLocalDateTime format()用法及代碼示例
- Java ChronoLocalDateTime with(TemporalAdjuster)用法及代碼示例
- Java ChronoLocalDateTime plus(TemporalAmount)用法及代碼示例
- Java ChronoLocalDateTime isAfter()用法及代碼示例
- Java ChronoLocalDateTime toEpochSecond()用法及代碼示例
- Java ChronoLocalDateTime compareTo()用法及代碼示例
注:本文由純淨天空篩選整理自srinam大神的英文原創作品 ChronoLocalDateTime toInstant() method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。