logo image

How to add given number of hours to Date Object in Java?


How to add given number of hours to Date Object in Java?

1 Answer

2 years ago by

Following program shows you how to increment a Date Object by given Hours

import java.util.Calendar;
import java.util.Date;

public class AddHoursToDateObject {

	public static void main(String[] args) {

		Date input = new Date();
		int hoursToAdd = 10;

		Calendar cal = Calendar.getInstance();
		cal.setTime(input);
		cal.add(Calendar.HOUR_OF_DAY, hoursToAdd);
		Date output = cal.getTime();

		System.out.println(output);

	}
}
2 years ago by Megamind