get date of Last Month - Java java.util

Java examples for java.util:Month

Description

get date of Last Month

Demo Code

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

public class Main {

    public static Date[] getLastMonth() {
        GregorianCalendar calendar = new GregorianCalendar();
        calendar.add(GregorianCalendar.MONTH, -1);
        calendar.set(GregorianCalendar.DATE, 1);
        calendar.set(GregorianCalendar.HOUR_OF_DAY, 0);
        calendar.set(GregorianCalendar.MINUTE, 0);
        calendar.set(GregorianCalendar.SECOND, 0);
        Date begin = calendar.getTime();

        calendar = new GregorianCalendar();
        calendar.set(GregorianCalendar.DATE, 1);
        calendar.add(GregorianCalendar.DATE, -1);
        calendar.set(GregorianCalendar.HOUR_OF_DAY, 23);
        calendar.set(GregorianCalendar.MINUTE, 59);
        calendar.set(GregorianCalendar.SECOND, 59);

        Date end = calendar.getTime();

        return new Date[] { begin, end };
    }/*from w w w . j  av  a  2  s . c o  m*/
}

Related Tutorials