当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Java ChronoZonedDateTime with(TemporalAdjuster)用法及代码示例


ChronoZonedDateTime接口的with(TemporalAdjuster Adjuster)方法用于使用TemporalAdjuster调整此日期时间,并在调整后返回调整后的日期时间的副本。使用指定的调节器策略对象进行调节。此ChronoZonedDateTime实例是不可变的,不受此方法调用的影响。一个简单的调节器用于设置一个字段,例如Year字段,而更复杂的调节器可能将时间设置为一年的最后一天。

用法:

default ChronoZonedDateTime with(TemporalAdjuster adjuster)

参数:该方法接受调节器作为要使用的调节器的参数。


返回值:此方法基于此返回ChronoZonedDateTime并进行调整。

异常:此方法引发以下异常:

  • DateTimeException–如果无法进行调整。
  • ArithmeticException–如果发生数字溢出。

以下示例程序旨在说明with()方法:

示例1:

// Java program to demonstrate 
// ChronoZonedDateTime.with() method 
  
import java.time.*; 
import java.time.chrono.*; 
import java.time.temporal.*; 
  
public class GFG { 
    public static void main(String[] args) 
    { 
  
        // create ChronoZonedDateTime object 
        ChronoZonedDateTime time 
            = ZonedDateTime 
                  .parse( 
                      "1918-10-25T23:12:38.543+02:00[Europe/Paris]"); 
  
        // print instance 
        System.out.println("ChronoZonedDateTime before"
                           + " adjustment: "
                           + time); 
  
        // apply with method of ChronoZonedDateTime 
        ChronoZonedDateTime updatedlocal 
            = time.with(Month.OCTOBER) 
                  .with(TemporalAdjusters 
                            .firstDayOfMonth()); 
  
        // print instance 
        System.out.println("ChronoZonedDateTime after"
                           + " adjustment: "
                           + updatedlocal); 
    } 
}
输出:
ChronoZonedDateTime before adjustment: 1918-10-25T23:12:38.543Z[Europe/Paris]
ChronoZonedDateTime after adjustment: 1918-10-01T23:12:38.543+01:00[Europe/Paris]

示例2:

// Java program to demonstrate 
// ChronoZonedDateTime.with() method 
  
import java.time.*; 
import java.time.chrono.*; 
import java.time.temporal.*; 
  
public class GFG { 
    public static void main(String[] args) 
    { 
  
        // create ChronoZonedDateTime object 
        ChronoZonedDateTime time 
            = ZonedDateTime 
                  .parse( 
                      "2018-12-06T19:21:12.123+05:30[Asia/Calcutta]"); 
  
        // print instance 
        System.out.println("ChronoZonedDateTime before"
                           + " adjustment: "
                           + time); 
  
        // apply with method of ChronoZonedDateTime 
        ChronoZonedDateTime updatedlocal 
            = time.with(Month.JANUARY) 
                  .with(TemporalAdjusters 
                            .firstDayOfMonth()); 
  
        // print instance 
        System.out.println("ChronoZonedDateTime after"
                           + " adjustment: "
                           + updatedlocal); 
    } 
}
输出:
ChronoZonedDateTime before adjustment: 2018-12-06T19:21:12.123+05:30[Asia/Calcutta]
ChronoZonedDateTime after adjustment: 2018-01-01T19:21:12.123+05:30[Asia/Calcutta]

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



相关用法


注:本文由纯净天空筛选整理自ShubhamMaurya3大神的英文原创作品 ChronoZonedDateTime with(TemporalAdjuster) method in Java with Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。