Java Date Clear clearTimeComponent(Date date)

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

Description

Given a Date object, returns a Date object for the same date but with the time component (hours, minutes, seconds & milliseconds) removed

License

Open Source License

Declaration

public static Date clearTimeComponent(Date date) 

Method Source Code


//package com.java2s;
/*//from   w ww. j  a v a 2s.co m
 * The contents of this file are subject to the OpenMRS Public License
 * Version 1.0 (the "License"); you may not use this file except in
 * compliance with the License. You may obtain a copy of the License at
 * http://license.openmrs.org
 *
 * Software distributed under the License is distributed on an "AS IS"
 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
 * License for the specific language governing rights and limitations
 * under the License.
 *
 * Copyright (C) OpenMRS, LLC.  All Rights Reserved.
 */

import java.util.Calendar;

import java.util.Date;

public class Main {
    /**
     * Given a Date object, returns a Date object for the same date but with the time component (hours, minutes, seconds & milliseconds) removed
     */
    public static Date clearTimeComponent(Date date) {
        // Get Calendar object set to the date and time of the given Date object
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);

        // Set time fields to zero
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);

        return cal.getTime();
    }
}

Related

  1. clearDisplayFileCreationDateCache(final String absolutePath)
  2. clearHourMinute(Date date)
  3. clearTime(Date date)
  4. clearTime(Date date)
  5. clearTime(GregorianCalendar date)