Close

Java Date Time - OffsetTime.adjustInto() Examples

Java Date Time Java Java API 


Class:

java.time.OffsetTime

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

Method:

public Temporal adjustInto(Temporal temporal)

This method adjusts the specified temporal object to have the same time and offset fields as this OffsetTime object. This method will throw UnsupportedTemporalTypeException if the provided temporal object does not support the OffsetTime fields.



Examples


package com.logicbig.example.offsettime;

import java.time.OffsetDateTime;
import java.time.OffsetTime;
import java.time.ZoneOffset;
import java.time.temporal.Temporal;

public class AdjustIntoExample {

public static void main(String... args) {
OffsetTime d = OffsetTime.of(17, 40, 33, 20000, ZoneOffset.ofHours(-6));
System.out.println(d);

OffsetDateTime d2 = OffsetDateTime.now();
System.out.println(d2);

Temporal t = d.adjustInto(d2);
System.out.println(t);
}
}

Output

17:40:33.000020-06:00
2017-05-01T15:54:48.440-05:00
2017-05-01T17:40:33.000020-06:00




package com.logicbig.example.offsettime;

import java.time.OffsetTime;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.time.temporal.Temporal;

public class AdjustIntoExample2 {

public static void main(String... args) {
OffsetTime d = OffsetTime.of(17, 40, 33, 20000, ZoneOffset.ofHours(-6));
System.out.println(d);

ZonedDateTime d2 = ZonedDateTime.now();
System.out.println(d2);

Temporal t = d.adjustInto(d2);
System.out.println(t);
}
}

Output

17:40:33.000020-06:00
2017-05-01T15:54:50.491-05:00[America/Chicago]
2017-05-01T17:40:33.000020-05:00[America/Chicago]




package com.logicbig.example.offsettime;

import java.time.LocalTime;
import java.time.OffsetTime;
import java.time.ZoneOffset;
import java.time.temporal.Temporal;

public class AdjustIntoExample3 {

public static void main(String... args) {
OffsetTime d = OffsetTime.of(17, 40, 33, 20000, ZoneOffset.ofHours(-6));
System.out.println(d);

LocalTime d2 = LocalTime.now();
System.out.println(d2);

Temporal t = d.adjustInto(d2);
System.out.println(t);
}
}

Output

Caused by: java.time.temporal.UnsupportedTemporalTypeException: Unsupported field: OffsetSeconds
at java.time.LocalTime.with(LocalTime.java:854)
at java.time.LocalTime.with(LocalTime.java:125)
at java.time.OffsetTime.adjustInto(OffsetTime.java:1123)
at com.logicbig.example.offsettime.AdjustIntoExample3.main(AdjustIntoExample3.java:22)
... 6 more




See Also