Java Date Set setTimePart(Date date, String time, Integer milliseconds)

Here you can find the source of setTimePart(Date date, String time, Integer milliseconds)

Description

Sets the time part of the given date by using pattern 'HHmmss', and sets the milliseconds.

License

Open Source License

Parameter

Parameter Description
date a parameter
time a parameter
milliseconds a parameter

Declaration

public static Date setTimePart(Date date, String time, Integer milliseconds) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.util.Calendar;

import java.util.Date;
import java.util.GregorianCalendar;

public class Main {
    /**/*from ww w .j  av  a 2s.c o  m*/
     * Sets the time part of given date with the given int type parameters
     * 
     * @param date
     * @param hour
     *            24 hour format
     * @param minute
     * @param second
     * @param millisecond
     * @return
     */
    public static Date setTimePart(Date date, int hour, int minute, int second, int millisecond) {
        GregorianCalendar endOfDay = new GregorianCalendar();
        endOfDay.setTime(date);
        // 24 saat seklindeki saat set ediliyor
        endOfDay.set(Calendar.HOUR_OF_DAY, hour);
        endOfDay.set(Calendar.MINUTE, minute);
        endOfDay.set(Calendar.SECOND, second);
        endOfDay.set(Calendar.MILLISECOND, millisecond);
        return endOfDay.getTime();
    }

    /**
     * Sets the time part of given date with the given int type parameters. Sets
     * the millisecond to 0 automatically
     * 
     * @param date
     *            24 hour format
     * @param hour
     * @param minute
     * @param second
     * @return
     */
    public static Date setTimePart(Date date, int hour, int minute, int second) {
        return setTimePart(date, hour, minute, second, 0);
    }

    /**
     * Sets the time part of given date with the given int type parameters. Sets
     * the millisecond to 0 automatically
     * 
     * @param date
     *            24 hour format
     * @param hourMinuteSecond
     *            ex: 91234 means: 09:12:34 (AM) ex: 145633 means: 14:56:33 (PM)
     * @return
     */
    public static Date setTimePart(Date date, int hourMinuteSecond) {
        int hour = hourMinuteSecond / 10000;
        int minute = (hourMinuteSecond - (10000 * hour)) / 100;
        int second = (hourMinuteSecond - (10000 * hour) - (100 * minute));
        return setTimePart(date, hour, minute, second, 0);
    }

    /**
     * Sets the time part of the given date by using pattern 'HHmmss', and sets
     * the milliseconds.
     * 
     * @param date
     * @param time
     * @param milliseconds
     * @return
     */
    public static Date setTimePart(Date date, String time, Integer milliseconds) {
        if (time == null)
            return null;
        if (time.length() != 6)
            return null;

        int hour = Integer.parseInt(time.substring(0, 2));
        int minute = Integer.parseInt(time.substring(2, 4));
        int second = Integer.parseInt(time.substring(4, 6));

        GregorianCalendar calendar = new GregorianCalendar();
        calendar.setTime(date);
        calendar.set(Calendar.HOUR_OF_DAY, hour);
        calendar.set(Calendar.MINUTE, minute);
        calendar.set(Calendar.SECOND, second);
        if (milliseconds != null)
            calendar.set(Calendar.MILLISECOND, milliseconds.intValue());

        return calendar.getTime();
    }
}

Related

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