Java Hour Add addHours(Date date, int hours)

Here you can find the source of addHours(Date date, int hours)

Description

Adds the number of hours to the specified java.util.Date Date object.

License

Open Source License

Parameter

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

Return

The new adjusted Date object

Declaration

public static Date addHours(Date date, int hours) 

Method Source Code


//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();
    }
}

Related

  1. addHH(Date date, int i)
  2. addHour(Date baseDate, int hour, TimeZone timezone)
  3. addHour(Date date, boolean up)
  4. addHour(java.util.Date d, int h)
  5. addHours(long date, int hours)