Java Calendar Truncate trunc(Calendar calendar)

Here you can find the source of trunc(Calendar calendar)

Description

Truncates a date object by setting to midnight of the current date.

License

Open Source License

Parameter

Parameter Description
calendar the date to truncate to midnight

Declaration

public static void trunc(Calendar calendar) 

Method Source Code

//package com.java2s;
/*/* www.  j  a va2  s .  c om*/
This file is part of Timelord.
Copyright 2005-2009 Jordan Reed
    
Timelord 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 3 of the License, or
(at your option) any later version.
    
Timelord 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 Timelord.  If not, see <http://www.gnu.org/licenses/>.
*/

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

public class Main {
    /**
     * Truncates a date object by setting to midnight of the
     * current date.
     *
     * @param inputDate the date to truncate to midnight
     * @return truncated date
     */
    public static Date trunc(Date inputDate) {
        Calendar outputCalendar = Calendar.getInstance();
        outputCalendar.setTime(inputDate);
        trunc(outputCalendar);
        return outputCalendar.getTime();
    }

    /**
     * Truncates a date object by setting to midnight of the
     * current date.
     *
     * @param calendar the date to truncate to midnight
     */
    public static void trunc(Calendar calendar) {
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
    }
}

Related

  1. trim(final Calendar cal)
  2. trimCalendar(Calendar cal)
  3. truncate(Calendar calendar)
  4. truncateCal(final Calendar cal)
  5. truncCalendar(Calendar c)
  6. truncCalendarToQuarter(Calendar c)