rewind Date To Beginning Of Month - Android java.util

Android examples for java.util:Month

Description

rewind Date To Beginning Of Month

Demo Code


//package com.java2s;

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

public class Main {
    public static Date rewindToBeginningOfMonth(Date date) {
        Calendar cal = buildFromDate(rewindToBeginningOfDay(date));
        cal.set(Calendar.DAY_OF_MONTH, 1);
        return cal.getTime();
    }//from www .  jav a 2  s .c  o  m

    private static Calendar buildFromDate(Date from) {
        Calendar c = Calendar.getInstance();
        c.setTime(from);
        return c;
    }

    public static Date rewindToBeginningOfDay(Date date) {
        Calendar cal = buildFromDate(date);
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);
        return cal.getTime();
    }
}

Related Tutorials