Q:

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

0

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

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Program

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

 

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now