📜  Java中的即时 atOffset() 方法和示例

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

Java中的即时 atOffset() 方法和示例

Instant 类atOffset(ZoneOffset offset)方法用于将这个瞬间与偏移量结合起来创建一个 OffsetDateTime 对象。此方法将 ZoneOffset 作为参数以返回 OffsetDateTime 对象,并且此 OffsetDataTime 对象是从该时刻在距 UTC/格林威治的指定偏移量处形成的。如果瞬间太大而无法适应偏移日期时间,则该方法将引发异常。此方法与OffsetDateTime.ofInstant(this, offset)相同。

句法:

public OffsetDateTime atOffset(ZoneOffset offset)

参数:
此方法接受一个参数偏移量,即 ZoneOffset 与此即时对象组合。它不应该为空

返回值:此方法返回从该瞬间和指定的 ZoneOffset 形成的偏移日期时间

异常:如果瞬间太大而无法适应偏移日期时间,则此方法将引发DateTimeException

下面的程序说明了 Instant.atOffset() 方法:
方案一:

// Java program to demonstrate
// Instant.atOffset() method
  
import java.time.*;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // create an instance object
        Instant instant
            = Instant.parse("2018-10-20T16:55:30.00Z");
  
        // print Instant Value
        System.out.println("Instant: "
                           + instant);
  
        // create a ZoneOffset object
        // with 7200 sec means 2 hours
        ZoneOffset offset = ZoneOffset.ofTotalSeconds(7200);
  
        // apply atOffset method to combine ZoneOffset
        // to this instant
        OffsetDateTime offsetDate = instant.atOffset(offset);
  
        // print results
        System.out.println("Offset Date and Time: "
                           + offsetDate);
    }
}
输出:
Instant: 2018-10-20T16:55:30Z
Offset Date and Time: 2018-10-20T18:55:30+02:00

方案二:

// Java program to demonstrate
// Instant.atOffset() method
  
import java.time.*;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // create an instance object
        Instant instant
            = Instant.parse("2018-10-20T16:55:30.00Z");
  
        // print Instant Value
        System.out.println("Instant: "
                           + instant);
  
        // create a ZoneOffset object
        // with 3 hours 45 minutes
        ZoneOffset offset
            = ZoneOffset.ofHoursMinutes(3, 45);
  
        // apply atOffset method to combine ZoneOffset
        // to this instant
        OffsetDateTime offsetDate
            = instant.atOffset(offset);
  
        // print results
        System.out.println("Offset Date and Time: "
                           + offsetDate);
    }
}
输出:
Instant: 2018-10-20T16:55:30Z
Offset Date and Time: 2018-10-20T20:40:30+03:45

方案 3:

// Java program to demonstrate
// Instant.atOffset() method
  
import java.time.*;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // create an instance object
        Instant instant
            = Instant.now();
  
        // print Instant Value
        System.out.println("Instant: "
                           + instant);
  
        // create a ZoneOffset Object
        // with 9 hours 45 minutes 30 second
        ZoneOffset offset
            = ZoneOffset
                  .ofHoursMinutesSeconds(9, 45, 30);
  
        // apply atOffset method to
        // combine ZoneOffset to this instant
        OffsetDateTime offsetDate
            = instant.atOffset(offset);
  
        // print results
        System.out.println("Offset Date and Time: "
                           + offsetDate);
    }
}
输出:
Instant: 2018-11-22T08:22:19.846Z
Offset Date and Time: 2018-11-22T18:07:49.846+09:45:30

参考: https: Java Java.time.ZoneOffset)