java.time.OffsetTime.withOffsetSameInstant() Method Example



Description

The java.time.OffsetTime.withOffsetSameInstant(ZoneOffset offset) method returns a copy of this OffsetTime with the specified offset ensuring that the result is at the same instant on an implied day.

Declaration

Following is the declaration for java.time.OffsetTime.withOffsetSameInstant(ZoneOffset offset) method.

public OffsetTime withOffsetSameInstant(ZoneOffset offset)

Parameters

offset − the zone offset to change to, not null.

Return Value

an OffsetTime based on this time with the requested offset, not null.

Example

The following example shows the usage of java.time.OffsetTime.withOffsetSameInstant(ZoneOffset offset) method.

package com.tutorialspoint;

import java.time.OffsetTime;
import java.time.ZoneOffset;

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

      OffsetTime time = OffsetTime.parse("10:15:30+01:00");
      OffsetTime result = time.withOffsetSameInstant(ZoneOffset.UTC);
      System.out.println(result);  
   }
}

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

09:15:30Z
Advertisements