Java Date Set setTime(final Date date, final int hours, final int minutes, final int seconds)

Here you can find the source of setTime(final Date date, final int hours, final int minutes, final int seconds)

Description

Given a Date as parameter, it sets it to the specified time and returns a copy.

License

Open Source License

Parameter

Parameter Description
date Original date
hours Hours to set to the date
minutes Minutes to set to the date
seconds Seconds to set to the date

Return

A new Date with the given time.

Declaration

public static Date setTime(final Date date, final int hours, final int minutes, final int seconds) 

Method Source Code

//package com.java2s;
/**/*from  w w  w . ja v  a2 s.c  om*/
 *
 * Licensed under the GNU General Public License (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.gnu.org/licenses/gpl.txt
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * 
 * @author Vadim Kisen
 *
 * copyright 2010 by uTest 
 */

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

public class Main {
    public static final int HOUR = Calendar.HOUR;
    public static final int MINUTE = Calendar.MINUTE;
    public static final int SECOND = Calendar.SECOND;

    /**
     * Given a Date as parameter, it sets it to the specified time and returns a
     * copy.
     * 
     * @param date
     *            Original date
     * @param hours
     *            Hours to set to the date
     * @param minutes
     *            Minutes to set to the date
     * @param seconds
     *            Seconds to set to the date
     * 
     * @return A new Date with the given time.
     */
    public static Date setTime(final Date date, final int hours, final int minutes, final int seconds) {
        final Calendar aux = Calendar.getInstance();
        aux.setTime(date);
        aux.set(Calendar.MILLISECOND, 0);
        aux.set(Calendar.HOUR, hours);
        aux.set(Calendar.MINUTE, minutes);
        aux.set(Calendar.SECOND, seconds);
        return aux.getTime();
    }
}

Related

  1. setTime(Date date, int hourOfDay, int minute, int second)
  2. setTime(Date date, int hours, int minutes, int seconds, int millis)
  3. setTime(Date date, String timeString)
  4. setTime(final Date date, final int hourOfDay, final int minute, final int second)
  5. setTime(final Date date, final int hourOfDay, final int minute, final int second, final int ms)
  6. setTimeForDate(Date date, int h, int m, int s)
  7. setTimePart(Date date, String time, Integer milliseconds)
  8. setTimeToNull(Date date)
  9. setTimeToZero(final Date date)