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).
Syntax:
public OffsetDateTime atOffset(ZoneOffset offset)
Parameters:
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:
Program 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
Program 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
Program 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
Reference: https://docs.oracle.com/javase/10/docs/api/java/time/Instant.html#atOffset(java.time.ZoneOffset)
Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.
Recommended Posts:
- LocalTime atOffset() method in Java with Examples
- Instant isSupported() method in Java with Examples
- Java 8 Clock instant() method with Examples
- Instant equals() method in Java with Examples
- Instant compareTo() method in Java with Examples
- Instant atZone() method in Java with Examples
- Instant getEpochSecond() method in Java with Examples
- Instant getLong() method in Java with Examples
- Instant getNano() method in Java with Examples
- Instant hashCode() method in Java with Examples
- Instant toEpochMilli() method in Java with Examples
- Instant isAfter() method in Java with Examples
- Instant isBefore() method in Java with Examples
- Instant from() method in Java with Examples
- Instant get() method in Java with Examples
- Instant minusMillis() method in Java with Examples
- Instant minusSeconds() method in Java with Examples
- Instant minusNanos() method in Java with Examples
- Instant plusNanos() method in Java with Examples
- Instant plusMillis() method in Java with Examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

