Friday, 15 April 2016

How to get Start Date and End Date for Year, Month, Week in java ?

package com.product.webapp.utilities;

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

public class DateUtils {

public static Calendar[] getDateIntervals(IntervalType type, Calendar reference) {
if (reference == null) {
reference = Calendar.getInstance();
}
Calendar startDate = (Calendar) reference.clone();
Calendar endDate = (Calendar) reference.clone();
if (type == IntervalType.CurrentYear) {
// first date of the Year
startDate.set(Calendar.DAY_OF_YEAR, 1);

// current date of the Year
endDate.set(Calendar.YEAR, endDate.get(Calendar.YEAR));
} else if (type == IntervalType.LastYear) {
// first date of the Year
startDate.set(Calendar.YEAR, startDate.get(Calendar.YEAR) - 1);

// Last date of the Year
startDate.set(Calendar.DAY_OF_YEAR, 1);

endDate.add(Calendar.YEAR, -1);
endDate.set(Calendar.DAY_OF_YEAR, endDate.getActualMaximum(Calendar.DAY_OF_YEAR));
} else if (type == IntervalType.CurrentMonth) {
// first date of the month
startDate.set(Calendar.DAY_OF_MONTH, 1);

// first date of the month
endDate.set(Calendar.YEAR, endDate.get(Calendar.YEAR));

} else if (type == IntervalType.LastMonth) {
// previous month
startDate.add(Calendar.MONTH, -1);
startDate.set(Calendar.DATE, 1);
// previous month, last date
endDate.add(Calendar.MONTH, -1);
endDate.set(Calendar.DAY_OF_MONTH, endDate.getActualMaximum(Calendar.DAY_OF_MONTH));
} else if (type == IntervalType.YesterDay) {
startDate.add(Calendar.DATE, -1);
endDate.add(Calendar.DATE, -1);
} else if (type == IntervalType.Today) {
startDate.add(Calendar.DATE, 0);
endDate.add(Calendar.DATE, 0);
} else if (type == IntervalType.Last3Month) {
// previous month
startDate.add(Calendar.MONTH, -3);
startDate.set(Calendar.DATE, 1);
// previous month, last date
endDate.add(Calendar.MONTH, -1);
endDate.set(Calendar.DAY_OF_MONTH, endDate.getActualMaximum(Calendar.DAY_OF_MONTH));
} else if (type == IntervalType.Last6Month) {
// previous month
startDate.add(Calendar.MONTH, -6);
startDate.set(Calendar.DATE, 1);
// previous month, last date
endDate.add(Calendar.MONTH, -1);
endDate.set(Calendar.DAY_OF_MONTH, endDate.getActualMaximum(Calendar.DAY_OF_MONTH));
} else if (type == IntervalType.CurrentWeek) {

// current Week
startDate.add(Calendar.DAY_OF_WEEK, startDate.getFirstDayOfWeek() - startDate.get(Calendar.DAY_OF_WEEK));
// previous Week, last date
endDate.set(Calendar.YEAR, endDate.get(Calendar.YEAR));

} else if (type == IntervalType.LastWeek) {
// previous week by convention (monday ... sunday)
// you will have to adjust this a bit if you want
// sunday to be considered as the first day of the week.
// start date : decrement until first sunday then
// down to monday
int dayOfWeek = startDate.get(Calendar.DAY_OF_WEEK);
while (dayOfWeek != Calendar.SUNDAY) {
startDate.add(Calendar.DATE, -1);
dayOfWeek = startDate.get(Calendar.DAY_OF_WEEK);
}
while (dayOfWeek != Calendar.MONDAY) {
startDate.add(Calendar.DATE, -1);
dayOfWeek = startDate.get(Calendar.DAY_OF_WEEK);
}

// end date , decrement until the first sunday
dayOfWeek = endDate.get(Calendar.DAY_OF_WEEK);
while (dayOfWeek != Calendar.SUNDAY) {
endDate.add(Calendar.DATE, -1);
dayOfWeek = endDate.get(Calendar.DAY_OF_WEEK);
}
} else {
new Exception();
}
return new Calendar[] { startDate, endDate };
}

public static Date[] getCurrentWeek(IntervalType type, Calendar reference) {
Calendar[] results = DateUtils.getDateIntervals(type, null);
return new Date[] { getFormattedFromDateTime(results[0].getTime()), getFormattedToDateTime(results[1].getTime()) };
}

public static Date[] getLastWeek(IntervalType type, Calendar reference) {
Calendar[] results = DateUtils.getDateIntervals(type, null);
return new Date[] { getFormattedFromDateTime(results[0].getTime()), getFormattedToDateTime(results[1].getTime()) };
}

public static Date[] getCurrentMonth(IntervalType type, Calendar reference) {
Calendar[] results = DateUtils.getDateIntervals(type, null);
return new Date[] { getFormattedFromDateTime(results[0].getTime()), getFormattedToDateTime(results[1].getTime()) };
}

public static Date[] getLastMonth(IntervalType type, Calendar reference) {
Calendar[] results = DateUtils.getDateIntervals(type, null);
return new Date[] { getFormattedFromDateTime(results[0].getTime()), getFormattedToDateTime(results[1].getTime()) };
}

public static Date[] getLast3Month(IntervalType type, Calendar reference) {
Calendar[] results = DateUtils.getDateIntervals(type, null);
return new Date[] { getFormattedFromDateTime(results[0].getTime()), getFormattedToDateTime(results[1].getTime()) };
}

public static Date[] getLast6Month(IntervalType type, Calendar reference) {
Calendar[] results = DateUtils.getDateIntervals(type, null);
return new Date[] { getFormattedFromDateTime(results[0].getTime()), getFormattedToDateTime(results[1].getTime()) };
}

public static Date[] getCurrentYear(IntervalType type, Calendar reference) {
Calendar[] results = DateUtils.getDateIntervals(type, null);
return new Date[] { getFormattedFromDateTime(results[0].getTime()), getFormattedToDateTime(results[1].getTime()) };
}

public static Date[] getLastYear(IntervalType type, Calendar reference) {
Calendar[] results = DateUtils.getDateIntervals(type, null);
return new Date[] { getFormattedFromDateTime(results[0].getTime()), getFormattedToDateTime(results[1].getTime()) };
}

public static Date[] getYesterDay(IntervalType type, Calendar reference) {
Calendar[] results = DateUtils.getDateIntervals(type, null);
return new Date[] { getFormattedFromDateTime(results[0].getTime()), getFormattedToDateTime(results[1].getTime()) };
}

public static Date[] getToday(IntervalType type, Calendar reference) {
Calendar[] results = DateUtils.getDateIntervals(type, null);
return new Date[] { getFormattedFromDateTime(results[0].getTime()), getFormattedToDateTime(results[1].getTime()) };
}

public static String getDateRangeNameByIntervalType(String intervalType) {
String dateName = "";
if (IntervalType.CurrentWeek.toString().equals(intervalType)) {
dateName = IntervalType.CurrentWeek.name();
} else if (IntervalType.LastWeek.toString().equals(intervalType)) {
dateName = IntervalType.LastWeek.name();
} else if (IntervalType.CurrentMonth.toString().equals(intervalType)) {
dateName = IntervalType.CurrentMonth.name();
} else if (IntervalType.LastMonth.toString().equals(intervalType)) {
dateName = IntervalType.LastMonth.name();
} else if (IntervalType.Last3Month.toString().equals(intervalType)) {
dateName = IntervalType.Last3Month.name();
} else if (IntervalType.Last6Month.toString().equals(intervalType)) {
dateName = IntervalType.Last6Month.name();
} else if (IntervalType.CurrentYear.toString().equals(intervalType)) {
dateName = IntervalType.CurrentYear.name();
} else if (IntervalType.LastYear.toString().equals(intervalType)) {
dateName = IntervalType.LastYear.name();
}
return dateName;
}

public static Date[] getDatesByIntervalType(String intervalType) {

Date[] dateRange = new Date[1];
if (IntervalType.CurrentWeek.toString().equals(intervalType)) {
dateRange = DateUtils.getCurrentWeek(IntervalType.CurrentWeek, null);
} else if (IntervalType.LastWeek.toString().equals(intervalType)) {
dateRange = DateUtils.getLastWeek(IntervalType.LastWeek, null);
} else if (IntervalType.CurrentMonth.toString().equals(intervalType)) {
dateRange = DateUtils.getCurrentMonth(IntervalType.CurrentMonth, null);
} else if (IntervalType.LastMonth.toString().equals(intervalType)) {
dateRange = DateUtils.getLastMonth(IntervalType.LastMonth, null);
} else if (IntervalType.Last3Month.toString().equals(intervalType)) {
dateRange = DateUtils.getLast3Month(IntervalType.Last3Month, null);
} else if (IntervalType.Last6Month.toString().equals(intervalType)) {
dateRange = DateUtils.getLast6Month(IntervalType.Last6Month, null);
} else if (IntervalType.CurrentYear.toString().equals(intervalType)) {
dateRange = DateUtils.getCurrentYear(IntervalType.CurrentYear, null);
} else if (IntervalType.LastYear.toString().equals(intervalType)) {
dateRange = DateUtils.getLastYear(IntervalType.LastYear, null);
} else if (IntervalType.YesterDay.toString().equals(intervalType)) {
dateRange = DateUtils.getYesterDay(IntervalType.YesterDay, null);
} else if (IntervalType.Today.toString().equals(intervalType)) {
dateRange = DateUtils.getToday(IntervalType.Today, null);
}

return dateRange;
}

private static Date getFormattedFromDateTime(Date date) {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.set(Calendar.HOUR_OF_DAY, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
return cal.getTime();
}

private static Date getFormattedToDateTime(Date date) {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.set(Calendar.HOUR_OF_DAY, 23);
cal.set(Calendar.MINUTE, 59);
cal.set(Calendar.SECOND, 59);
return cal.getTime();
}

public static Date removeTime(Date date) {
       Calendar cal = Calendar.getInstance();
       cal.setTime(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();
   }
}