/**
* get period for last year
* @param time
* @return
*/
public static DatePeriodDTO getLastYear(long time) {
Calendar calendar = Calendar.getInstance();
//get last year
calendar.add(Calendar.YEAR, -1);
int lastYear = calendar.get(Calendar.YEAR);
calendar.clear();
//set to last year
calendar.set(Calendar.YEAR, lastYear);
//get first day of last year
Date yearFrom = calendar.getTime();
//get last day of last year
calendar.roll(Calendar.DAY_OF_YEAR, -1);
Date yearTo = calendar.getTime();
return new DatePeriodDTO(yearFrom, yearTo);
}
/**
* get period for last month
* @param time
* @return
*/
public static DatePeriodDTO getLastMonth(long time) {
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.MONTH, -1);
//last day of last month
calendar.set(Calendar.DAY_OF_MONTH,calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
Date strDateTo = calendar.getTime();
//first day of last month
calendar.set(Calendar.DAY_OF_MONTH,calendar.getActualMinimum(Calendar.DAY_OF_MONTH));
Date strDatefrom = calendar.getTime();
return new DatePeriodDTO(strDatefrom, strDateTo);
}
/**
* get period for last week
* @param time
* @return
*/
public static DatePeriodDTO getLastWeek(long time) {
Calendar calendar = Calendar.getInstance();
calendar.setFirstDayOfWeek(Calendar.MONDAY);
//last Sunday
calendar.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
calendar.add(Calendar.WEEK_OF_MONTH, -1);
Date endDate = calendar.getTime();
//last Monday
calendar.set(Calendar.DATE, calendar.get(Calendar.DATE) - 6);
Date startDate = calendar.getTime();
return new DatePeriodDTO(startDate, endDate);
}
/**
* is last year
* @param time
* @return
*/
public static boolean isLastYear(long time) {
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.YEAR, -1);
int lastYear = calendar.get(Calendar.YEAR);
calendar.setTime(new Date(time));
int paramYear = calendar.get(Calendar.YEAR);
if (paramYear == lastYear) {
return true;
}
return false;
}
/**
* is last month
* @param time
* @return
*/
public static boolean isLastMonth(long time) {
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.MONTH, -1);
int lastMonth = calendar.get(Calendar.MONTH);
calendar.setTime(new Date(time));
int paramMonth = calendar.get(Calendar.MONTH);
if (paramMonth == lastMonth) {
return true;
}
return false;
}
/**
* is last week
* @param time
* @return
*/
public static boolean isLastWeek(long time) {
Calendar calendar = Calendar.getInstance();
calendar.setFirstDayOfWeek(Calendar.MONDAY);
calendar.add(Calendar.DATE, -7);
calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
int lastWeek = calendar.get(Calendar.WEEK_OF_YEAR);
calendar.setTime(new Date(time));
int paramWeek = calendar.get(Calendar.WEEK_OF_YEAR);
if (paramWeek == lastWeek) {
return true;
}
return false;
}