Java Date Set setTimeToZero(final Date date)

Here you can find the source of setTimeToZero(final Date date)

Description

set Time To Zero

License

Open Source License

Return

Returns the passed date with the same year/month/day but with the time set to 00:00:00.000

Declaration

public static Date setTimeToZero(final Date date) 

Method Source Code

//package com.java2s;
/*//from   w  w  w  . j a  v  a2s .c o m
  TimeUtil.java / Freenet
  Copyright (C) 2005-2006 The Free Network project
  This program is free software; you can redistribute it and/or
  modify it under the terms of the GNU General Public License as
  published by the Free Software Foundation; either version 2 of
  the License, or (at your option) any later version.
    
  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  General Public License for more details.
    
  You should have received a copy of the GNU General Public License
  along with this program; if not, write to the Free Software
  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

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

import java.util.TimeZone;

public class Main {
    /**
     * @return Returns the passed date with the same year/month/day but with the time set to 00:00:00.000
     */
    public static Date setTimeToZero(final Date date) {
        // We need to cut off the hour/minutes/seconds
        final GregorianCalendar calendar = new GregorianCalendar(TimeZone.getTimeZone("UTC"));
        calendar.setTimeInMillis(date.getTime()); // We must not use setTime(date) in case the date is not UTC.
        calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH),
                0, 0, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTime();
    }
}

Related

  1. setTime(final Date date, final int hourOfDay, final int minute, final int second, final int ms)
  2. setTime(final Date date, final int hours, final int minutes, final int seconds)
  3. setTimeForDate(Date date, int h, int m, int s)
  4. setTimePart(Date date, String time, Integer milliseconds)
  5. setTimeToNull(Date date)
  6. setTimeZero(Date dt)
  7. setToDayStartTime(Date date)
  8. setToLastDayInMonth(final Date date)
  9. setToMidnight(Date d)