Java Date Adjust adjustToDayStartAndEnd(Date from, Date to)

Here you can find the source of adjustToDayStartAndEnd(Date from, Date to)

Description

Set a period to day start and end.

License

Open Source License

Parameter

Parameter Description
from a parameter
to a parameter

Declaration

public static void adjustToDayStartAndEnd(Date from, Date to) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) Nov 2, 2012 NetXForge.//from  w w w.j  a va  2 s.  co m
 * 
 * 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/>
 * 
 * Contributors: Christophe Bouhier - initial API and implementation and/or
 * initial documentation
 *******************************************************************************/

import java.util.Calendar;

import java.util.Date;

public class Main {
    /**
     * Set a period to day start and end.
     * 
     * @param from
     * @param to
     */
    public static void adjustToDayStartAndEnd(Date from, Date to) {
        adjustToDayStart(from);
        adjustToDayEnd(to);
    }

    /**
     * Set the hour, minutes, seconds and milliseconds so the {@link Calendar}
     * represents midnight, which is the start of the day.
     * 
     * @param cal
     */
    public static void adjustToDayStart(Calendar cal) {
        // When doing this, we push it forward one day, so if the day is 7 Jan
        // at 11:50:27,
        // it will become 7 Jan at 00:00:00h, so we substract one day.
        cal.add(Calendar.DAY_OF_MONTH, -1);
        cal.set(Calendar.HOUR_OF_DAY, 24);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);
    }

    /**
     * Adjust a {@link Date} to the start of the day.
     * 
     * @param d
     * @return
     */
    public static Date adjustToDayStart(Date d) {
        final Calendar cal = Calendar.getInstance();
        cal.setTime(d);
        adjustToDayStart(cal);
        d.setTime(cal.getTime().getTime());
        return cal.getTime();
    }

    /**
     * Set the hours, minutes, seconds and milliseconds so the calendar
     * represents midnight minus one milli-second.
     * 
     * @param cal
     */
    public static void adjustToDayEnd(Calendar cal) {
        cal.set(Calendar.HOUR_OF_DAY, 23);
        cal.set(Calendar.MINUTE, 59);
        cal.set(Calendar.SECOND, 59);
        cal.set(Calendar.MILLISECOND, 999);
    }

    public static Date adjustToDayEnd(Date d) {
        final Calendar cal = Calendar.getInstance();
        cal.setTime(d);
        adjustToDayEnd(cal);
        d.setTime(cal.getTime().getTime());
        return cal.getTime();
    }
}

Related

  1. adjustDate(Date dtDate, int iYears, int iMonths, int iDays, int iHours, int iMinutes, int iSeconds)
  2. adjustDays(Date startingDate, int deltaDays)
  3. adjustHour(Date dt, int hour)
  4. adjustHourAndMinute(Date n)
  5. adjustTime(Date date, int hour, int minute, int second)
  6. getAdjustDayResetTime(Date date, int adjustAmount)
  7. getAdjustedDate(Date original, int millisecondAdjustment)
  8. getTimeWithAdjustment(Date date, boolean carryover)