📌  相关文章
📜  Java中的 ChronoZonedDateTime with(TemporalField, long) 方法和示例

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

Java中的 ChronoZonedDateTime with(TemporalField, long) 方法和示例

ChronoZonedDateTime接口的with(TemporalField field, long newValue)方法用于将 ChronoZonedDateTime 的指定字段设置为新值,并返回新时间的副本。此方法可用于更改任何支持的字段,例如年、日、月、小时、分钟或秒。如果由于不支持该字段或由于某些其他原因而无法设置新值,则会引发异常。这个 ChronoZonedDateTime 实例是不可变的,不受此方法调用的影响。

句法:

ChronoZonedDateTime with(TemporalField field, 
                         long newValue)

参数:此方法接受两个参数:

  • 字段,这是要在结果中设置的字段和
  • newValue其中将结果中字段的新值作为参数。

返回值:此方法基于此返回一个ChronoZonedDateTime ,并带有指定的字段集。

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

  • DateTimeException – 如果无法进行调整。
  • UnsupportedTemporalTypeException – 如果不支持该字段。
  • 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(
                      "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(ChronoField.YEAR, 2017);
  
        // 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: 2017-12-06T19:21:12.123+05:30[Asia/Calcutta]

方案二:

// 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(ChronoField.MONTH_OF_YEAR, 1);
  
        // print instance
        System.out.println("ChronoZonedDateTime after"
                           + " adjustment: "
                           + updatedlocal);
    }
}
输出:
ChronoZonedDateTime before adjustment: 1918-10-25T23:12:38.543Z[Europe/Paris]
ChronoZonedDateTime after adjustment: 1918-01-25T23:12:38.543Z[Europe/Paris]

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