w3resource logo


Java Programming Exercies

Java DateTime, Calendar Exercises: Add some hours to the current time

Secondary Nav

Java DateTime, Calendar : Exercise-15 with Solution

Write a Java program to add some hours to the current time.

Java Code:

import java.time.*;
public class Exercise15 {
   public static void main(String[] args)
    {
      LocalTime time = LocalTime.now(); 
      // adding four hours
      LocalTime newTime = time.plusHours(4);
      System.out.println();
      System.out.println("Time after 2 hours : " + newTime);
      System.out.println();
    }
}

Sample Output :

Time after 2 hours : 17:05:26.929

Java Practice online:

import java.time.*;
public class Exercise15 {
   public static void main(String[] args)
    {
      LocalTime time = LocalTime.now(); 
      // adding four hours
      LocalTime newTime = time.plusHours(4);
      System.out.println();
      System.out.println("Time after 2 hours : " + newTime);
      System.out.println();
    }
}



Join our Question Answer community to learn and share your programming knowledge.