Close

Java Date Time - OffsetDateTime.withHour() 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 OffsetDateTime withHour(int hour)

Returns a copy of this OffsetDateTime with the hour-of-day changed with the provided hour.

If the provided 'hour' is not valid, this method will throw java.time.DateTimeException. The valid values are 0 - 23.



Examples


package com.logicbig.example.offsetdatetime;

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

public class WithHourExample {

public static void main(String... args) {
OffsetDateTime d = OffsetDateTime.of(2015, 10, 5, 15,
30, 10, 1000000, ZoneOffset.ofHours(-5));
System.out.println(d);

OffsetDateTime d2 = d.withHour(11);
System.out.println(d2);
}
}

Output

2015-10-05T15:30:10.001-05:00
2015-10-05T11:30:10.001-05:00




See Also