Instant類的AdjustInto(Temporal時間)方法將傳遞的時間對象調整為具有應用此方法的此瞬間。
用法:
public Temporal adjustInto(Temporal temporal)
參數:該方法接受作為要調整的目標時間對象的參數時間。它不能為空。
返回值:此方法返回調整後的時間對象。
異常:此方法引發以下異常:
- DateTimeException如果此方法無法進行調整。
- ArithmeticException如果調整時發生數字溢出。
以下示例程序旨在說明Instant.adjustInto()方法:
示例1:
// Java program to demonstrate
// Instant.adjustInto() method
import java.time.*;
import java.time.temporal.Temporal;
public class GFG {
public static void main(String[] args)
{
// create an instance object
Instant instant
= Instant.parse("2018-11-20T16:55:30.00Z");
// create a Temporal object
// which is equal to OffsetDateTime object
OffsetDateTime passTemporal
= OffsetDateTime.now();
// print passed Value
System.out.println("Passed Value: "
+ passTemporal);
// apply adjustInto method
// to adjust OffsetDateTime Temporal
// to instant object
Temporal returnTemporal
= instant.adjustInto(passTemporal);
// print results
System.out.println("Returned Value: "
+ (OffsetDateTime)returnTemporal);
}
}
輸出:
Passed Value: 2018-11-22T09:22:17.297Z Returned Value: 2018-11-20T16:55:30Z
示例2:
// Java program to demonstrate
// Instant.adjustInto() method
import java.time.*;
import java.time.temporal.Temporal;
public class GFG {
public static void main(String[] args)
{
// create an instance object
Instant instant
= Instant.parse("2018-11-17T06:50:39.00Z");
// create a Temporal object
// which is equal to ZonedDateTime object
ZonedDateTime passTemporal
= ZonedDateTime.now();
// print passed Value
System.out.println("Passed Value: "
+ passTemporal);
// apply adjustInto method
// to adjust ZonedDateTime Temporal
// to instant object
Temporal returnTemporal
= instant.adjustInto(passTemporal);
// print results
System.out.println("Returned Value: "
+ (ZonedDateTime)returnTemporal);
}
}
輸出:
Passed Value: 2018-11-22T09:22:20.995Z[Etc/UTC] Returned Value: 2018-11-17T06:50:39Z[Etc/UTC]
示例3:
// Java program to demonstrate
// Instant.adjustInto() method
import java.time.*;
import java.time.temporal.Temporal;
public class GFG {
public static void main(String[] args)
{
// create an instance object
Instant instant
= Instant.parse("2017-11-01T16:25:00.00Z");
// create a Temporal object
// which is equal to Instant object
// with current Instant
Temporal passTemporal
= Instant.now();
// print passed Value
System.out.println("Passed Value: "
+ passTemporal);
// apply adjustInto method to adjust Temporal
// to this instant object
Temporal returnTemporal
= instant.adjustInto(passTemporal);
// print results
System.out.println("Returned Value: "
+ returnTemporal);
}
}
輸出:
Passed Value: 2018-11-22T09:22:23.298Z Returned Value: 2017-11-01T16:25:00Z
參考:https://docs.oracle.com/javase/10/docs/api/java/time/Instant.html#adjustInto(java.time.temporal.Temporal)
相關用法
- Java Month adjustInto()用法及代碼示例
- Java LocalDate adjustInto()用法及代碼示例
- Java Year adjustInto()用法及代碼示例
- Java DayOfWeek adjustInto()用法及代碼示例
- Java ChronoLocalDateTime adjustInto()用法及代碼示例
- Java ChronoLocalDate adjustInto()用法及代碼示例
- Java MonthDay adjustInto()用法及代碼示例
- Java LocalTime adjustInto()用法及代碼示例
- Java OffsetTime adjustInto()用法及代碼示例
- Java OffsetDateTime adjustInto()用法及代碼示例
- Java ZoneOffset adjustInto(Temporal)用法及代碼示例
- Java Instant get()用法及代碼示例
- Java Instant plus()用法及代碼示例
- Java Instant now()用法及代碼示例
- Java Instant from()用法及代碼示例
注:本文由純淨天空篩選整理自AmanSingh2210大神的英文原創作品 Instant adjustInto() method in Java with Example。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。