OffsetDateTime getOffset() method in Java with examples

The getOffset() method of OffsetDateTime class in Java gets the zone offset, such as ‘+05:00’.

Syntax :

public ZoneOffset getOffset()

Parameter : This method accepts does not accepts any parameter.



Return Value: It returns the zone offset, not null.

Below programs illustrate the getOffset() method:

Program 1 :

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate the getOffset() method
  
import java.time.OffsetDateTime;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // parses a date
        OffsetDateTime date = OffsetDateTime.parse("2018-12-03T12:30:30+01:00");
  
        // Prints the offset of given date
        System.out.println("offset: " + date.getOffset());
    }
}

chevron_right


Output:

offset: +01:00

Program 2 :

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate the getOffset() method
  
import java.time.OffsetDateTime;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // parses a date
        OffsetDateTime date = OffsetDateTime.parse("2016-10-03T12:30:30+06:20");
  
        // Prints the offset of given date
        System.out.println("Offset: " + date.getOffset());
    }
}

chevron_right


Output:

Offset: +06:20

Reference: https://docs.oracle.com/javase/8/docs/api/java/time/OffsetDateTime.html#getOffset–



My Personal Notes arrow_drop_up

Recommended Posts: