LocalDateTime類adjustInto()方法
- adjustInto() 方法可在
java.time
包。 - adjustInto() 方法用於將此 LocalDateTime 對象調整為給定的 Temporal 對象,或者換句話說,我們可以說調整後的對象具有此對象表示的相同日期和時間。
- adjustInto() 方法是一個非靜態方法,它隻能通過類對象訪問,如果我們嘗試使用類名訪問方法,那麽我們將得到一個錯誤。
-
adjustInto() 方法可能會在調整 Date-Time 時拋出異常。
- DateTimeException - 當給定參數無法調整此 LocalDateTime 時,可能會引發此異常。
- ArithmeticException - 當計算結果超過此日期的長度時,可能會引發此異常。
用法:
public Temporal adjustInto(Temporal temp);
參數:
Temporal temp
- 表示要調整此 LocalDateTime 的時間對象。
返回值:
這個方法的返回類型是Temporal
,它返回 Temporal 對象,該對象保存將此對象日期時間調整為給定對象的值。
例:
// Java program to demonstrate the example
// of adjustInto(Temporal temp) method
// of LocalDateTime
import java.time.*;
public class AdjustIntoOfLocalDateTime {
public static void main(String args[]) {
// Instantiates two LocalDateTime
// and a ZonedDateTime object
LocalDateTime da_ti1 = LocalDateTime.parse("2005-10-05T10:10:10");
LocalDateTime da_ti2 = LocalDateTime.now();
ZonedDateTime zo_da_time = ZonedDateTime.parse("2008-08-08T20:20:20.213+03:00[Africa/Asmara]");
// Display da_ti1, da_ti2
// and zo_da_time
System.out.println("LocalDateTime da_ti1 and da_ti2:");
System.out.println("da_ti1:" + da_ti1);
System.out.println("da_ti2:" + da_ti2);
System.out.println();
System.out.println("ZonedDateTime zo_da_time:");
System.out.println("zo_da_time:" + zo_da_time);
System.out.println();
// Here, this method adjusts this object
// into the given object i.e. here we are
// adjusting this LocalDateTime(da_ti1)
// into the given object ZonedDateTime(zo_da_time)
// and the given object is to have same
// date and time as this object da_ti1
zo_da_time = (ZonedDateTime) da_ti1.adjustInto(zo_da_time);
// Display updated zo_da_time
System.out.println("da_ti1.adjustInto(zo_da_time):" + zo_da_time);
// Here, this method adjusts this object
// into the given object i.e. here we are
// adjusting this LocalDateTime(da_ti2)
// into the given object ZonedDateTime(zo_da_time)
// and the given object is to have same
// date and time as this object da_ti2
zo_da_time = (ZonedDateTime) da_ti2.adjustInto(zo_da_time);
// Display updated zo_da_time
System.out.println("da_ti2.adjustInto(zo_da_time):" + zo_da_time);
}
}
輸出
LocalDateTime da_ti1 and da_ti2: da_ti1:2005-10-05T10:10:10 da_ti2:2020-06-04T07:51:48.984048 ZonedDateTime zo_da_time: zo_da_time:2008-08-08T20:20:20.213+03:00[Africa/Asmara] da_ti1.adjustInto(zo_da_time):2005-10-05T10:10:10+03:00[Africa/Asmara] da_ti2.adjustInto(zo_da_time):2020-06-04T07:51:48.984048+03:00[Africa/Asmara]
相關用法
- Java LocalDateTime atZone()用法及代碼示例
- Java LocalDateTime atOffset()用法及代碼示例
- Java LocalDateTime minusHours()用法及代碼示例
- Java LocalDateTime plusHours()用法及代碼示例
- Java LocalDateTime getNano()用法及代碼示例
- Java LocalDateTime plus()用法及代碼示例
- Java LocalDateTime minusDays()用法及代碼示例
- Java LocalDateTime getDayOfYear()用法及代碼示例
- Java LocalDateTime getDayOfWeek()用法及代碼示例
- Java LocalDateTime withHour()用法及代碼示例
- Java LocalDateTime plusYears()用法及代碼示例
- Java LocalDateTime isSupported()用法及代碼示例
- Java LocalDateTime toLocalDate()用法及代碼示例
- Java LocalDateTime hashCode()用法及代碼示例
- Java LocalDateTime isAfter()用法及代碼示例
- Java LocalDateTime plusSeconds()用法及代碼示例
- Java LocalDateTime getHour()用法及代碼示例
- Java LocalDateTime ofEpochSecond()用法及代碼示例
- Java LocalDateTime ofInstant()用法及代碼示例
- Java LocalDateTime withMinute()用法及代碼示例
注:本文由純淨天空篩選整理自 Java LocalDateTime Class | adjustInto() Method with Example。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。