java.time.OffsetDateTime.ofInstant() Method Example



Description

The java.time.OffsetDateTime.ofInstant(Instant instant, ZoneId zone) method Obtains an instance of OffsetDateTime from an Instant and zone ID.

Declaration

Following is the declaration for java.time.OffsetDateTime.ofInstant(Instant instant, ZoneId zone) method.

public static OffsetDateTime ofInstant(Instant instant, ZoneId zone)

Parameters

  • instant − the instant to create the date-time from, not null

  • zone − the time-zone, which may be an offset, not null

Return Value

the offset date-time, not null.

Exceptions

DateTimeException − if the result exceeds the supported range.

Example

The following example shows the usage of java.time.OffsetDateTime.of(LocalDateTime date, ZoneOffset offset) method.

package com.tutorialspoint;

import java.time.Instant;
import java.time.OffsetDateTime;
import java.time.ZoneId;

public class OffsetDateTimeDemo {
   public static void main(String[] args) {

      Instant instant = Instant.now();
      OffsetDateTime date = OffsetDateTime.ofInstant(instant,ZoneId.systemDefault());
      System.out.println(date);  
   }
}

Let us compile and run the above program, this will produce the following result −

2017-03-21T12:49:52.134+05:30
Advertisements