Java Date Set stripTime(Date date)

Here you can find the source of stripTime(Date date)

Description

Strips the time component from a date.

License

Mozilla Public License

Parameter

Parameter Description
date Original date.

Return

Date without the time component.

Declaration

public static Date stripTime(Date date) 

Method Source Code

//package com.java2s;
/**/*w w w. j a  va 2 s  . c  om*/
 * This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
 * If a copy of the MPL was not distributed with this file, You can obtain one at
 * http://mozilla.org/MPL/2.0/.
 * 
 * This Source Code Form is also subject to the terms of the Health-Related Additional
 * Disclaimer of Warranty and Limitation of Liability available at
 * http://www.carewebframework.org/licensing/disclaimer.
 */

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

public class Main {
    /**
     * Strips the time component from a date.
     * 
     * @param date Original date.
     * @return Date without the time component.
     */
    public static Date stripTime(Date date) {
        return addDays(date, 0, true);
    }

    /**
     * Adds specified number of days to date and, optionally strips the time component.
     * 
     * @param date Date value to process.
     * @param daysOffset # of days to add.
     * @param stripTime If true, strip the time component.
     * @return Input value the specified operations applied.
     */
    public static Date addDays(Date date, int daysOffset, boolean stripTime) {
        if (date == null) {
            return null;
        }

        Calendar calendar = Calendar.getInstance();
        calendar.setLenient(false); // Make sure the calendar will not perform
        // automatic correction.
        calendar.setTime(date); // Set the time of the calendar to the given
        // date.

        if (stripTime) { // Remove the hours, minutes, seconds and milliseconds.
            calendar.set(Calendar.HOUR_OF_DAY, 0);
            calendar.set(Calendar.MINUTE, 0);
            calendar.set(Calendar.SECOND, 0);
            calendar.set(Calendar.MILLISECOND, 0);
        }

        calendar.add(Calendar.DAY_OF_MONTH, daysOffset);
        return calendar.getTime();

    }
}

Related

  1. setToMidnight(Date d)
  2. setYear(Date d, int year)
  3. setYear(Date date, int year)
  4. stripTime(Date d)
  5. stripTime(Date date)
  6. stripTime(Date time)
  7. stripTimeFromDate(Date aDate)