Close

Java Date Time - OffsetTime.with() 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

Methods:

public OffsetTime with(TemporalAdjuster adjuster)

Returns an adjusted copy of this OffsetTime. The adjustment logic is provided by the 'adjuster'.



public OffsetTime with(TemporalField field,
                       long newValue)

Returns a copy of this OffsetTime with the specified field set to a new value.


Examples


package com.logicbig.example.offsettime;

import java.time.OffsetTime;
import java.time.ZoneOffset;
import java.time.temporal.Temporal;
import java.time.temporal.TemporalAdjuster;
import java.time.temporal.TemporalAdjusters;

public class WithExample {

public static void main(String... args) {

OffsetTime d = OffsetTime.of(18, 29, 32, 300000000,
ZoneOffset.ofHours(-5));
System.out.println(d);

OffsetTime d2 = d.with(temporal -> {
//adjusting to different timezone
OffsetTime t = OffsetTime.from(temporal);
return OffsetTime.of(t.getHour(), t.getMinute(), t.getSecond(), t.getNano(),
ZoneOffset.ofHours(-6));
});
System.out.println(d2);

}
}

Output

18:29:32.300-05:00
18:29:32.300-06:00




package com.logicbig.example.offsettime;

import java.time.OffsetTime;
import java.time.temporal.ChronoField;
import java.time.temporal.UnsupportedTemporalTypeException;

public class WithExample2 {

public static void main(String... args) {
OffsetTime t = OffsetTime.now();
System.out.println(t);

for (ChronoField chronoField : ChronoField.values()) {
try {
OffsetTime t2 = t.with(chronoField, 1);
System.out.printf("With %16s: %s%n", chronoField, t2);
} catch (UnsupportedTemporalTypeException e) {
System.out.printf(" -- Not supported for %s%n", chronoField);
}
}
}
}

Output

16:12:57.229-05:00
With NanoOfSecond: 16:12:57.000000001-05:00
With NanoOfDay: 00:00:00.000000001-05:00
With MicroOfSecond: 16:12:57.000001-05:00
With MicroOfDay: 00:00:00.000001-05:00
With MilliOfSecond: 16:12:57.001-05:00
With MilliOfDay: 00:00:00.001-05:00
With SecondOfMinute: 16:12:01.229-05:00
With SecondOfDay: 00:00:01.229-05:00
With MinuteOfHour: 16:01:57.229-05:00
With MinuteOfDay: 00:01:57.229-05:00
With HourOfAmPm: 13:12:57.229-05:00
With ClockHourOfAmPm: 13:12:57.229-05:00
With HourOfDay: 01:12:57.229-05:00
With ClockHourOfDay: 01:12:57.229-05:00
With AmPmOfDay: 16:12:57.229-05:00
-- Not supported for DayOfWeek
-- Not supported for AlignedDayOfWeekInMonth
-- Not supported for AlignedDayOfWeekInYear
-- Not supported for DayOfMonth
-- Not supported for DayOfYear
-- Not supported for EpochDay
-- Not supported for AlignedWeekOfMonth
-- Not supported for AlignedWeekOfYear
-- Not supported for MonthOfYear
-- Not supported for ProlepticMonth
-- Not supported for YearOfEra
-- Not supported for Year
-- Not supported for Era
-- Not supported for InstantSeconds
With OffsetSeconds: 16:12:57.229+00:00:01




See Also