Java Instant atOffset()方法及实例

Java Instant atOffset()方法及实例

Instant类的 atOffset(ZoneOffset offset) 方法是用来将这个瞬间与一个偏移量结合起来,以创建一个OffsetDateTime对象。该方法以ZoneOffset为参数,返回一个OffsetDateTime对象,这个OffsetDataTime对象是由这个瞬间与UTC/Greenwich的指定偏移量形成的。如果该时刻太大,无法装入一个偏移日期时间,那么该方法将抛出一个异常。该方法与 OffsetDateTime.ofInstant(this, offset) 相同

语法

public OffsetDateTime atOffset(ZoneOffset offset)

参数:

这个方法接受一个参数 offset ,它是要与这个即时对象结合的ZoneOffset。它不应该是空的。

返回值: 该方法返回由这个即时对象和指定的ZoneOffset形成的 偏移日期时间

异常: 如果瞬时太大,不能放入一个偏移日期时间,该方法会抛出 DateTimeException

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

程序1 :

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

程序2

// 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://docs.oracle.com/javase/10/docs/api/java/time/Instant.html#atOffset(java.time.ZoneOffset)

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程