Instant类的atOffset(ZoneOffset offset)方法用于将此瞬间与偏移量结合在一起以创建OffsetDateTime对象。此方法将ZoneOffset作为参数返回一个OffsetDateTime对象,并且此OffsetDataTime对象是从此时刻开始,与UTC /格林威治指定的偏移量而形成的。如果瞬时值太大而无法适应偏移日期时间,则该方法将引发异常。此方法与OffsetDateTime.ofInstant(this,offset)相同。
用法:
public OffsetDateTime atOffset(ZoneOffset 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)
相关用法
- Java LocalTime atOffset()用法及代码示例
- Java Instant from()用法及代码示例
- Java Instant get()用法及代码示例
- Java Instant plus()用法及代码示例
- Java Instant with()用法及代码示例
- Java Instant until()用法及代码示例
- Java Instant now()用法及代码示例
- Java 8 Clock instant()用法及代码示例
- Java Instant minusSeconds()用法及代码示例
- Java Instant getLong()用法及代码示例
- Java Instant plusSeconds()用法及代码示例
- Java Instant hashCode()用法及代码示例
- Java Instant toEpochMilli()用法及代码示例
- Java Instant minus()用法及代码示例
- Java Instant getNano()用法及代码示例
注:本文由纯净天空筛选整理自AmanSingh2210大神的英文原创作品 Instant atOffset() method in Java with Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。