Program to Add Given Hours in Current Date and Time in Java

You are currently viewing Program to Add Given Hours in Current Date and Time in Java

Program to add given hours in current date and time in Java

package IncludeHelp;

import java.util.Calendar;

public class ExAddHrsToCurrentDate {
  public static void main(String[] args) {
    // create object of calendar class.
    Calendar now = Calendar.getInstance();

    // fetch the Current date and time of the system.
    System.out.println("Current Date : " + (now.get(Calendar.MONTH) + 1) +
      "-" + now.get(Calendar.DATE) + "-" + now.get(Calendar.YEAR));
    System.out.println("Current time : " + now.get(Calendar.HOUR_OF_DAY) +
      ":" + now.get(Calendar.MINUTE) + ":" + now.get(Calendar.SECOND));

    // take default hours.
    now.add(Calendar.HOUR, 15);

    //printng date after adding 15 hours in time
    System.out.println("New Date : " + (now.get(Calendar.MONTH) + 1) + "-" +
      now.get(Calendar.DATE) + "-" + now.get(Calendar.YEAR));
    // This will show new time and date after adding hours.
    System.out.println("New time after adding 15 hours : " + now.get(Calendar.HOUR_OF_DAY) + ":" + now.get(Calendar.MINUTE) + ":" + now.get(Calendar.SECOND));
  }
}

Output

Current Date : 12-23-2017
Current time : 20:36:24
New Date : 12-24-2017
New time after adding 15 hours : 11:36:24

Ranjith

Hi, I'm Manoj a full-time Blogger, YouTuber, Affiliate Marketer, & founder of Coding Diksha. Here, I post about programming to help developers.

Leave a Reply