This method sets to starting day of the month for the given date - Java java.util

Java examples for java.util:Month

Description

This method sets to starting day of the month for the given date

Demo Code


//package com.java2s;

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

public class Main {
    /**//from   w ww .  ja v a2  s.co m
     * This method sets to starting day of the month for the given date
     *
     * @param date
     *            date
     * @return Calendar starting date of the given month
     */
    public static Calendar setToFirstOfMonth(final Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.set(Calendar.DAY_OF_MONTH, 1);
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);
        return cal;
    }

    /**
     * This method sets to starting day of the Calendar month
     *
     * @param cal
     *            Calendar
     * @return Calendar First Date of the month
     */
    public static Calendar setToFirstOfMonth(final Calendar cal) {
        cal.set(Calendar.DAY_OF_MONTH, 1);
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);
        return cal;
    }
}

Related Tutorials