📌  相关文章
📜  带有示例的Java中的 ChronoZonedDateTime with(TemporalAdjuster) 方法

📅  最后修改于: 2022-05-13 01:54:24.409000             🧑  作者: Mango

带有示例的Java中的 ChronoZonedDateTime with(TemporalAdjuster) 方法

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

句法:

default ChronoZonedDateTime with(TemporalAdjuster adjuster)

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

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

异常:此方法抛出以下异常:

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

下面的程序说明了 with() 方法:

方案一:

// 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]

方案二:

// 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-