Java Calendar Month getStartOfMonth(Date day, Calendar cal)

Here you can find the source of getStartOfMonth(Date day, Calendar cal)

Description

get Start Of Month

License

Apache License

Declaration

public static Date getStartOfMonth(Date day, Calendar cal) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

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

public class Main {
    /**//from w  w  w  .  j  a v a  2  s.  c o m
     * Returns a Date set to the first possible millisecond of the month, just
     * after midnight. If a null day is passed in, a new Date is created.
     * midnight (00m 00h 00s)
     */
    public static Date getStartOfMonth(Date day) {
        return getStartOfMonth(day, Calendar.getInstance());
    }

    public static Date getStartOfMonth(Date day, Calendar cal) {
        if (day == null)
            day = new Date();
        cal.setTime(day);

        // set time to start of day
        cal.set(Calendar.HOUR_OF_DAY, cal.getMinimum(Calendar.HOUR_OF_DAY));
        cal.set(Calendar.MINUTE, cal.getMinimum(Calendar.MINUTE));
        cal.set(Calendar.SECOND, cal.getMinimum(Calendar.SECOND));
        cal.set(Calendar.MILLISECOND, cal.getMinimum(Calendar.MILLISECOND));

        // set time to first day of month
        cal.set(Calendar.DAY_OF_MONTH, 1);

        return cal.getTime();
    }
}

Related

  1. getMonthStart(Calendar c)
  2. getMonthStart(Calendar cal)
  3. getMonthStr(Calendar cal)
  4. getNextMonth(Calendar calendar, int month)
  5. getNumDaysInMonth(GregorianCalendar aCalendar)
  6. incrementMonth(Calendar cal)
  7. incrementMonthByVal(Calendar theCal, int val)
  8. intToCalendarMonth(int month)
  9. isFirstDayOfMonth(Calendar calendar)