This method returns the first day of the month of the specified date - Java java.util

Java examples for java.util:Month

Description

This method returns the first day of the month of the specified date

Demo Code


//package com.java2s;
import java.util.Calendar;
import java.util.Date;

public class Main {
    /**/*w w w  .j av  a  2 s  .c  o m*/
     * This method returns the first day of the month of the specified date
     *
     * @param date
     * @return
     */
    public static Date getFirstDayOfMonth(Date date) {
        Date d = getMinHour(date);

        Calendar calendar = Calendar.getInstance();
        calendar.setTime(d);
        calendar.set(Calendar.DAY_OF_MONTH, 1);

        return calendar.getTime();
    }

    /**
     * This method returns the specified date at 00h00min00sec
     *
     * @param date
     * @return
     */
    public static Date getMinHour(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTime();
    }
}

Related Tutorials