📌  相关文章
📜  Java中的 OffsetDateTime with() 方法及示例

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

Java中的 OffsetDateTime with() 方法及示例

在 OffsetDateTime 类中,根据传递给它的参数,有两种类型的 with() 方法。

with(TemporalAdjuster 调节器)

OffsetDateTime类的with(TemporalAdjusteradjuster)方法用于使用 TemporalAdjuster 调整此 OffsetDateTime,调整后返回调整后的 OffsetDateTime 的副本。使用指定的调整器策略对象进行调整。此 OffsetDateTime 实例是不可变的,不受此方法调用的影响。

句法:

public OffsetDateTime with(TemporalAdjuster adjuster)

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

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

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

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

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

// Java program to demonstrate
// OffsetDateTime.with() method
  
import java.time.*;
import java.time.temporal.*;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // create a OffsetDateTime object
        OffsetDateTime off
            = OffsetDateTime.parse("2018-12-12T13:30:30+05:00");
  
        // print instance
        System.out.println("OffsetDateTime before"
                           + " adjustment: "
                           + off);
  
        // apply with method of OffsetDateTime class
        OffsetDateTime updatedlocal
            = off.with(Month.JUNE)
                  .with(TemporalAdjusters
                            .firstDayOfMonth());
  
        // print instance
        System.out.println("OffsetDateTime after"
                           + " adjustment: "
                           + updatedlocal);
    }
}
输出:
OffsetDateTime before adjustment: 2018-12-12T13:30:30+05:00
OffsetDateTime after adjustment: 2018-06-01T13:30:30+05:00

with(TemporalField field, long newValue)

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

句法:

public OffsetDateTime with(TemporalField field, long newValue)

参数:此方法接受作为结果中要设置的字段的字段和结果中字段的新值作为参数的newValue

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

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

  • DateTimeException – 如果无法进行调整。
  • UnsupportedTemporalTypeException – 如果不支持该字段。
  • ArithmeticException – 如果发生数字溢出。

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

// Java program to demonstrate
// OffsetDateTime.with() method
  
import java.time.*;
import java.time.temporal.*;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // create a OffsetDateTime object
        OffsetDateTime off
            = OffsetDateTime.parse("2018-12-12T13:30:30+05:00");
  
        // print instance
        System.out.println("OffsetDateTime before"
                           + " adjustment: "
                           + off);
  
        // apply with method of OffsetDateTime class
        OffsetDateTime updatedlocal
            = off.with(ChronoField.DAY_OF_MONTH, 31);
  
        // print instance
        System.out.println("OffsetDateTime after"
                           + " applying method: "
                           + updatedlocal);
    }
}
输出:
OffsetDateTime before adjustment: 2018-12-12T13:30:30+05:00
OffsetDateTime after applying method: 2018-12-31T13:30:30+05:00

参考:
Java Java )
https://docs.oracle.com/javase/10/docs/api/java Java Java, long)