Close

Java Date Time - OffsetDateTime.atZoneSimilarLocal() Examples

Java Date Time Java Java API 


Class:

java.time.OffsetDateTime

java.lang.Objectjava.lang.Objectjava.time.OffsetDateTimejava.time.OffsetDateTimejava.time.temporal.TemporalTemporaljava.time.temporal.TemporalAdjusterTemporalAdjusterjava.lang.ComparableComparablejava.io.SerializableSerializableLogicBig

Method:

public ZonedDateTime atZoneSimilarLocal(ZoneId zone)

This method combines this date-time with a time-zone to create a new instance of ZonedDateTime. This method, instead of adjusting the time/date to the target zone, keeps the same local date and time.



Examples


package com.logicbig.example.offsetdatetime;

import java.time.OffsetDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;

public class AtZoneSimilarLocalExample {

public static void main (String... args) {
OffsetDateTime d = OffsetDateTime.of(2016, 10, 20,
11, 12, 30, 1000,
ZoneOffset.ofHours(-6));
ZonedDateTime z = d.atZoneSimilarLocal(ZoneId.of("Asia/Seoul"));
System.out.println(z);

}
}

Output

2016-10-20T11:12:30.000001+09:00[Asia/Seoul]




This example demonstrate the difference between atZoneSimilarLocal() and atZoneSameInstant.

package com.logicbig.example.offsetdatetime;

import java.time.OffsetDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;

public class AtZoneSimilarLocalExample2 {

public static void main (String... args) {
OffsetDateTime d = OffsetDateTime.of(2016, 10, 20,
11, 12, 30, 1000,
ZoneOffset.ofHours(-5));

System.out.printf("Original OffSetDateTime: %s%n", d);

ZonedDateTime z = d.atZoneSimilarLocal(ZoneId.of("Asia/Seoul"));
System.out.printf("atZoneSimilarLocal: %s%n", z);

z = d.atZoneSameInstant(ZoneId.of("Asia/Seoul"));
System.out.printf("atZoneSameInstant: %s%n", z);
}
}

Output

Original OffSetDateTime: 2016-10-20T11:12:30.000001-05:00
atZoneSimilarLocal: 2016-10-20T11:12:30.000001+09:00[Asia/Seoul]
atZoneSameInstant: 2016-10-21T01:12:30.000001+09:00[Asia/Seoul]




This example demonstrates that atZoneSimilarLocal adjusts the DST (Daylight saving time) gap/overlap to the resultant local time and offset.

Offset -5 represents USA central region during DST and offset -6 also represents the same region but outside of DST. Notice the resultant Offset of Asia/Seoul remains the same in both cases.

package com.logicbig.example.offsetdatetime;

import java.time.OffsetDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;

public class AtZoneSimilarLocalExample3 {


public static void main (String... args) {
OffsetDateTime d = OffsetDateTime.of(2016, 11, 6,
0, 20, 30, 1000,
ZoneOffset.ofHours(-5));

System.out.printf("Original OffSetDateTime in DST: %s%n", d);

ZonedDateTime z = d.atZoneSimilarLocal(ZoneId.of("Asia/Seoul"));
System.out.printf("atZoneSimilarLocal: %s%n", z);

d = OffsetDateTime.of(2016, 11, 6,
4, 20, 30, 1000,
ZoneOffset.ofHours(-6));

System.out.printf("Original OffSetDateTime after DST: %s%n", d);

z = d.atZoneSimilarLocal(ZoneId.of("Asia/Seoul"));
System.out.printf("atZoneSimilarLocal: %s%n", z);
}
}

Output

Original OffSetDateTime in DST: 2016-11-06T00:20:30.000001-05:00
atZoneSimilarLocal: 2016-11-06T00:20:30.000001+09:00[Asia/Seoul]
Original OffSetDateTime after DST: 2016-11-06T04:20:30.000001-06:00
atZoneSimilarLocal: 2016-11-06T04:20:30.000001+09:00[Asia/Seoul]




See Also