Java Calendar Day getLastDayOfPreviousPeriod(int startDayOfPeriod, Calendar cal)

Here you can find the source of getLastDayOfPreviousPeriod(int startDayOfPeriod, Calendar cal)

Description

get Last Day Of Previous Period

License

Apache License

Declaration

public static Date getLastDayOfPreviousPeriod(int startDayOfPeriod, Calendar cal) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

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

public class Main {
    public static Date getLastDayOfPreviousPeriod(int startDayOfPeriod) {
        Calendar cal = Calendar.getInstance();
        return getLastDayOfPreviousPeriod(startDayOfPeriod, cal);
    }//w w  w.  j av  a2  s.co m

    public static Date getLastDayOfPreviousPeriod(int startDayOfPeriod, Calendar cal) {
        int huidigeDagVdMaand = cal.get(Calendar.DAY_OF_MONTH);
        if (huidigeDagVdMaand >= startDayOfPeriod) {
            int diff = huidigeDagVdMaand - startDayOfPeriod;
            cal.add(Calendar.DAY_OF_MONTH, -(diff + 1));
        } else {
            cal.add(Calendar.DAY_OF_MONTH, -huidigeDagVdMaand);
            while (cal.get(Calendar.DAY_OF_MONTH) >= startDayOfPeriod) {
                cal.add(Calendar.DAY_OF_MONTH, -1);
            }
        }
        cal.clear(Calendar.MILLISECOND);
        cal.clear(Calendar.SECOND);
        cal.clear(Calendar.MINUTE);
        cal.clear(Calendar.HOUR);
        return cal.getTime();
    }
}

Related

  1. getEndOfTodayAsCalendar()
  2. getFirstDay(Calendar cal)
  3. getGregorianDay(Calendar cal)
  4. getJulianDay(Calendar currentDate)
  5. getLastDay(Date targetDate, Calendar cal)
  6. getMonday(Calendar calendar)
  7. getNextDay(Calendar cal)
  8. getNextDay(Calendar calendar, int day)
  9. getNextMonday(Calendar date)