Java - Offset Time and Datetime


OffsetTime and OffsetDateTime classes represents a time and a datetime, respectively, with a fixed zone offset from UTC.

An offset time and datetime have no knowledge of a time zone.

Examples of an offset time and an offset datetime in the ISO-8601 format are 10:50:11+5:30 and 2012-05-11T10:50:11+5:30, respectively.

The relationships between local and offset dates and times can be represented as follows:

OffsetTime = LocalTime + ZoneOffset
OffsetDateTime = LocalDateTime + ZoneOffset

You can always extract a LocalXxx from an OffsetXxx.

An OffsetDateTime stores an instant on the timeline, and hence, conversion between OffsetDateTime and Instant is supported.

Using Offset Dates, Times, and Datetimes


import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalTime;
import java.time.OffsetDateTime;
import java.time.OffsetTime;
import java.time.ZoneId;
import java.time.ZoneOffset;

public class Main {
  public static void main(String[] args) {
    // Get the current offset time
    OffsetTime ot1 =;
    System.out.println("Current offset time: " + ot1);

    // Create a zone offset +05:30
    ZoneOffset offset = ZoneOffset.ofHoursMinutes(5, 30);

    // Create an offset time
    OffsetTime ot2 = OffsetTime.of(16, 40, 28, 0, offset);
    System.out.println("An offset time: " + ot2);

    // Get the current offset date time
    OffsetDateTime odt1 =;
    System.out.println("Current offset datetime: " + odt1);

    // Create an offset date time
    OffsetDateTime odt2 = OffsetDateTime.of(2012, 5, 11, 18, 10, 30, 0, offset);
    System.out.println("An offset datetime: " + odt2);

    // Get the local date and time from the offset date time
    LocalDate ld1 = odt1.toLocalDate();
    LocalTime lt1 = odt1.toLocalTime();
    System.out.println("Current Local Date: " + ld1);
    System.out.println("Current Local Time: " + lt1);

    // Get the instant from the offset date time
    Instant i1 = odt1.toInstant();
    System.out.println("Current Instant: " + i1);

    // Create an offset date time from the instant
    ZoneId usChicago = ZoneId.of("America/Chicago");
    OffsetDateTime odt3 = OffsetDateTime.ofInstant(i1, usChicago);
    System.out.println("Offset datetime from instant: " + odt3);
  }/*w  ww.  ja  va 2 s .  c o m*/