Java SQL Time truncateTimePartOfDate(Date date)

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

Description

Treats the input date as a date with UTC time and truncates the time part.

License

Open Source License

Declaration

private static Date truncateTimePartOfDate(Date date) 

Method Source Code

//package com.java2s;
/** -----------------------------------------------------------------
 *    Sammelbox: Collection Manager - A free and open-source collection manager for Windows & Linux
 *    Copyright (C) 2011 Jerome Wagener & Paul Bicheler
 *
 *    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 3 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, see <http://www.gnu.org/licenses/>.
 ** ----------------------------------------------------------------- */

import java.sql.Date;

import java.util.Calendar;
import java.util.TimeZone;

public class Main {
    /** Treats the input date as a date with UTC time and truncates the time part. Truncating means the time part is set to all zeroes.
     *  If treated of a timezone time may differ from expected result!*/
    private static Date truncateTimePartOfDate(Date date) {
        if (date == null) {
            // A null date is possible in the case where the user hasn't selected a date yet.
            // Thus, we need to abort in this scenario.
            return null;
        }//from  w w  w  .j  av  a 2s . c  om

        Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
        cal.setTime(date);
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);
        long timeAsMillis = cal.getTimeInMillis();
        Date truncatedDate = new Date(timeAsMillis);

        return truncatedDate;

    }
}

Related

  1. toDateFromTime(String time)
  2. todayBeginTime()
  3. toGMTTime(long local)
  4. toMySQLDate(LocalDateTime dateTime)
  5. toPOSIXTime(String timeString)
  6. unformattedFromTime(java.sql.Time t)