Subversion Repositories bacoAlunos

Rev

Rev 206 | Blame | Compare with Previous | Last modification | View Log | RSS feed

package jomm.utils;

import java.util.Date;
import java.text.SimpleDateFormat;

/**
 * @author Jorge Machado
 * @date 15/Mar/2008
 * @time 15:04:18
 * @see jomm.utils
 */

public class DatesUtils
{
    /**
     * @param untilYear until Year
     * @param untilMonth until Month
     * @param untilDay until Day
     * @param days to go back
     * @return Return the start date calculating the go back days in choosed until date
     */

    public static 5+0%2Fdocs%2Fapi+Date">Date getStartDate(int untilYear, int untilMonth, int untilDay, int days)
    {
        MyCalendar c = new MyCalendar(untilYear,untilMonth, untilDay);
        5+0%2Fdocs%2Fapi+Date">Date endDate = c.getTime();
        return new 5+0%2Fdocs%2Fapi+Date">Date(c.getTimeInMillis() - ((long)days * 24 * 60 * 60 * 1000));
    }

    /**
     *
      * @param untilYear until Year
     * @param untilMonth until Month
     * @param months to go back
     * @return the start date going back m months. The returned date allays is placed in first day of the month
     */

    public static 5+0%2Fdocs%2Fapi+Date">Date getStartDate(int untilYear, int untilMonth, int months)
    {
        MyCalendar c = new MyCalendar(untilYear,untilMonth, 1);
        int dif = untilMonth - months;
        if(dif == 0)
        {
            return new MyCalendar(untilYear,1,1).getTime();
        }
        else if(dif > 0)
        {
            return new MyCalendar(untilYear,untilMonth-dif,1).getTime();
        }
        else
        {
            int div = dif / 12;
            int rest = dif % 12;
            if(rest < 0)
                rest *= -1;
            if(div < 0)
                div *= -1;
            int year = untilYear - 1 - div;
            int month = 12 - rest;
            return new MyCalendar(year,month,1).getTime();
        }
    }

   

}