Java Calendar Adjust adjustToMonthStart(Calendar cal)

Here you can find the source of adjustToMonthStart(Calendar cal)

Description

Adjust a Date to the start of the month (and start of the day).

License

Open Source License

Parameter

Parameter Description
beginTime a parameter

Declaration

public static void adjustToMonthStart(Calendar cal) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) Nov 2, 2012 NetXForge.//  w  w  w. jav  a2 s  .  c  o  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 {
    /**
     * Adjust a {@link Date} to the start of the month (and start of the day).
     * 
     * @param beginTime
     * @return
     */
    public static void adjustToMonthStart(Calendar cal) {
        cal.set(Calendar.DAY_OF_MONTH, 1);
    }

    public static Date adjustToMonthStart(Date date) {
        final Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        adjustToMonthStart(cal);
        adjustToDayStart(cal);
        date.setTime(cal.getTime().getTime());
        return cal.getTime();
    }

    /**
     * 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();
    }
}

Related

  1. adjustCalendarToLocalTime(Calendar cal)
  2. adjustDate(Calendar calendar, int differenceInDay)
  3. adjustedMillis(Calendar cal)
  4. adjustToDayEnd(Calendar cal)
  5. adjustToFieldStart(Calendar cal, int[] fields)
  6. getMidnightCalendarByUnadjustedDate(Date date, TimeZone timezone)
  7. truncCalendarToWeek(Calendar c, int adjustIncrement)
  8. truncCalendarToWeek(Calendar c, int adjustIncrement)