java.time.OffsetDateTime.withOffsetSameLocal() Method Example



Description

The java.time.OffsetDateTime.withOffsetSameLocal(ZoneOffset offset) method Returns a copy of this OffsetDateTime with the specified offset ensuring that the result has the same local date-time.

Following is the declaration for java.time.OffsetDateTime.withOffsetSameLocal(ZoneOffset offset) method.

public OffsetDateTime withOffsetSameLocal(ZoneOffset offset)

Parameters

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

Return Value

an OffsetDateTime based on this date-time with the requested offset, not null.

Exceptions

DateTimeException − if the result exceeds the supported date range.

Example

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

package com.tutorialspoint;

import java.time.OffsetDateTime;
import java.time.ZoneOffset;

public class OffsetDateTimeDemo {
   public static void main(String[] args) {
      
      OffsetDateTime date = OffsetDateTime.parse("2017-01-03T10:15:30+01:00");
      OffsetDateTime result = date.withOffsetSameLocal(ZoneOffset.UTC);
      System.out.println(result);  
   }
}

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

2017-01-03T10:15:30Z
Advertisements