Here you can find the source of addHours(Date date, int hours)
Parameter | Description |
---|---|
date | The Date object that we want to add time to |
hours | The number of hours to be added to the given Date object |
public static Date addHours(Date date, int hours)
//package com.java2s; /* /*from ww w.j ava 2 s .c o m*/ * Licensed Materials - Property of IBM ? Copyright IBM Corporation 2015. All * Rights Reserved. This sample program is provided AS IS and may be used, * executed, copied and modified without royalty payment by customer (a) for its * own instruction and study, (b) in order to develop applications designed to * run with an IBM product, either for customer's own internal use or for * redistribution by customer, as part of such an application, in customer's own * products. */ import java.util.Calendar; import java.util.Date; import java.util.GregorianCalendar; public class Main { /** * Adds the number of hours to the specified {@link java.util.Date Date} object. * * @param date The Date object that we want to add time to * @param hours The number of hours to be added to the given Date object * @return The new adjusted Date object */ public static Date addHours(Date date, int hours) { Calendar temp = new GregorianCalendar(); temp.setTime(date); temp.set(Calendar.HOUR_OF_DAY, hours); return temp.getTime(); } }