Instant atOffset() method in Java with Examples
The atOffset(ZoneOffset offset) method of Instant class is used to combine this instant with an offset to create an OffsetDateTime object. This method takes ZoneOffset as a parameter to return an OffsetDateTime object and this OffsetDataTime object is formed from this instant at the specified offset from UTC/Greenwich. If the instant is too large to fit into an offset date-time then the method will throw an exception. This method is same as OffsetDateTime.ofInstant(this, offset).
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.
public OffsetDateTime atOffset(ZoneOffset offset)
This method accepts one parameter offset which is the ZoneOffset to combine with this instant object. It should not be null
Return Value: This method returns the offset date-time formed from this instant and the specified ZoneOffset.
Exception: This method throws DateTimeException if the instant is too large to fit into an offset date-time.
Below programs illustrate the Instant.atOffset() method:
Instant: 2018-10-20T16:55:30Z Offset Date and Time: 2018-10-20T18:55:30+02:00
Instant: 2018-10-20T16:55:30Z Offset Date and Time: 2018-10-20T20:40:30+03:45
Instant: 2018-11-22T08:22:19.846Z Offset Date and Time: 2018-11-22T18:07:49.846+09:45:30