OffsetDateTime withHour() method in Java with examples

The withHour() method of OffsetDateTime class in Java returns a copy of this OffsetDateTime with the hour of the day altered as specified in the parameter.

Syntax:

public OffsetDateTime withHour(int hour)

Parameter: This method accepts a single parameter hour which specifies the hour of the day to be set in the result which can range from 0 to 23.

Return Value: It returns a OffsetDateTime based on this date with the requested hour of the day and not null.

Exceptions: The program throws a DateTimeException when the hour of the day value is invalid or if the day-of-year is invalid.

Below programs illustrate the withHour() method:

Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate the withHour() method
  
import java.time.OffsetDateTime;
import java.time.ZonedDateTime;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // Parses the date1
        OffsetDateTime date1
            = OffsetDateTime
                  .parse(
                      "2018-12-12T13:30:30+05:00");
  
        // Prints dates
        System.out.println("Date1: " + date1);
  
        // Changes the hour of day
        System.out.println("Date1 after altering hour of the day: "
                           + date1.withHour(20));
    }
}

chevron_right


Output:

Date1: 2018-12-12T13:30:30+05:00
Date1 after altering hour of the day: 2018-12-12T20:30:30+05:00

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate the withHour() method
  
import java.time.OffsetDateTime;
  
public class GFG {
    public static void main(String[] args)
    {
        try {
            // Parses the date1
            OffsetDateTime date1
                = OffsetDateTime
                      .parse(
                          "2018-12-12T13:30:30+05:00");
  
            // Prints dates
            System.out.println("Date1: " + date1);
  
            // Changes the hour of day
            System.out.println("Date1 after altering hour of the day: "
                               + date1.withHour(27));
        }
        catch (Exception e) {
            System.out.println("Exception: " + e);
        }
    }
}

chevron_right


Output:

Date1: 2018-12-12T13:30:30+05:00
Exception: java.time.DateTimeException:
           Invalid value for HourOfDay (valid values 0 - 23): 27

Reference: https://docs.oracle.com/javase/10/docs/api/java/time/OffsetDateTime.html#withHour(int)



My Personal Notes arrow_drop_up

The function of education is to teach one to think intensively and to think critically

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.