Date and Time

LocalTime plusHours() method in Java Code with Examples

Table of Contents

Program

import java.time.LocalDateTime;
import java.text.ParseException;
public class Hours 
{
    public static void main(String[] args) throws ParseException 
    {
        LocalDateTime dt1 = LocalDateTime.parse("2018-01-11T10:15:30");
        LocalDateTime dt2=dt1.plusHours(2);
        System.out.println("After 2 Hours added: "+dt2);
    }
}

Output

After 2 Hours added: 2018-01-11T12:15:30

Description

public LocalDateTime plusHours​(long hours)

Returns a copy of this LocalDateTime with the specified number of hours added.
This instance is immutable and unaffected by this method call.

Parameters:

hours – the hours to add, may be negative

Returns:

a LocalDateTime based on this date-time with the hours added, not null

Throws:

DateTimeException – if the result exceeds the supported date range