當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Java ChronoLocalDateTime with(TemporalAdjuster)用法及代碼示例


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

參考: https://docs.oracle.com/javase/9/docs/api/java/time/chrono/ChronoLocalDateTime.html#with-java.time.temporal.TemporalAdjuster-



相關用法


注:本文由純淨天空篩選整理自srinam大神的英文原創作品 ChronoLocalDateTime with(TemporalAdjuster) method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。