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


The code you provided is a Java program that uses the java.time package to add 10 hours to the current local time and print the result. Here is a breakdown of the code:

  • The java.time package is imported at the beginning of the code.
  • The Add_Hours class is defined, which contains the main method.
  • Within the main method, the LocalTime.now() method is called to get the current local time and store it in the y variable.
  • The plusHours() method is called on the y variable with an argument of 10, which adds 10 hours to the current local time and stores the result in the nt variable.
  • The System.out.println() method is called to print the string "Time After 2 Hours : " concatenated with the value of the nt variable, which represents the local time after 10 hours.

Note that the string "Time After 2 Hours : " in the System.out.println() method should be changed to "Time After 10 Hours : " to accurately reflect the added time.

Source Code

import java.time.*;
public class Add_Hours
{
	public static void main(String[] args)
	{
		LocalTime y = LocalTime.now();
		LocalTime nt = y.plusHours(10);
		System.out.println("Time After 2 Hours : " + nt);
	}
}

Output

Time After 2 Hours : 23:53:02.303817200

Example Programs