ChronoLocalDateTime接口的with(TemporalAdjuster Adjuster)方法用於使用TemporalAdjuster調整此日期時間,調整後返回調整後的日期時間的副本。使用指定的調節器策略對象進行調節。該ChronoLocalDateTime實例是不可變的,不受此方法調用的影響。一個簡單的調節器用於設置一個字段,例如Year字段,而更複雜的調節器可能將時間設置為一年的最後一天。
用法:
default ChronoLocalDateTime with(TemporalAdjuster adjuster)
參數:該方法接受調節器作為要使用的調節器的參數。
返回值:此方法根據所做的調整返回一個ChronoLocalDateTime。
異常:此方法引發以下異常:
- DateTimeException–如果無法進行調整。
- ArithmeticException–如果發生數字溢出。
以下示例程序旨在說明with()方法:
示例1:
// Java program to demonstrate
// ChronoLocalDateTime.with() method
import java.time.*;
import java.time.chrono.*;
import java.time.temporal.*;
public class GFG {
public static void main(String[] args)
{
// create ChronoLocalDateTime object
ChronoLocalDateTime time
= LocalDateTime
.parse("2019-12-31T19:15:30");
// print instance
System.out.println("ChronoLocalDateTime before"
+ " adjustment: "
+ time);
// apply with method of ChronoLocalDateTime
ChronoLocalDateTime updatedlocal
= time.with(Month.OCTOBER)
.with(TemporalAdjusters
.firstDayOfMonth());
// print instance
System.out.println("ChronoLocalDateTime after"
+ " adjustment: "
+ updatedlocal);
}
}
輸出:
ChronoLocalDateTime before adjustment: 2019-12-31T19:15:30 ChronoLocalDateTime after adjustment: 2019-10-01T19:15:30
示例2:
// Java program to demonstrate
// ChronoLocalDateTime.with() method
import java.time.*;
import java.time.chrono.*;
import java.time.temporal.*;
public class GFG {
public static void main(String[] args)
{
// create ChronoLocalDateTime object
ChronoLocalDateTime time
= LocalDateTime
.parse("2018-10-25T23:12:31.123");
// print instance
System.out.println("ChronoLocalDateTime before"
+ " adjustment: "
+ time);
// apply with method of ChronoLocalDateTime
ChronoLocalDateTime updatedlocal
= time.with(Month.JANUARY)
.with(TemporalAdjusters
.firstDayOfMonth());
// print instance
System.out.println("ChronoLocalDateTime after"
+ " adjustment: "
+ updatedlocal);
}
}
輸出:
ChronoLocalDateTime before adjustment: 2018-10-25T23:12:31.123 ChronoLocalDateTime after adjustment: 2018-01-01T23:12:31.123
相關用法
- Java ChronoLocalDateTime get()用法及代碼示例
- Java ChronoLocalDateTime until()用法及代碼示例
- Java ChronoLocalDateTime from()用法及代碼示例
- Java ChronoLocalDateTime getChronology()用法及代碼示例
- Java ChronoLocalDateTime compareTo()用法及代碼示例
- Java ChronoLocalDateTime toEpochSecond()用法及代碼示例
- Java ChronoLocalDateTime equals()用法及代碼示例
- Java ChronoLocalDateTime toLocalTime()用法及代碼示例
- Java ChronoLocalDateTime hashCode()用法及代碼示例
- Java ChronoLocalDateTime isBefore()用法及代碼示例
- Java ChronoLocalDateTime isEqual()用法及代碼示例
- Java ChronoLocalDateTime plus(TemporalAmount)用法及代碼示例
- Java ChronoLocalDateTime toInstant()用法及代碼示例
- Java ChronoLocalDateTime format()用法及代碼示例
- Java ChronoLocalDateTime isAfter()用法及代碼示例
注:本文由純淨天空篩選整理自srinam大神的英文原創作品 ChronoLocalDateTime with(TemporalAdjuster) method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。