Java Utililty Methods Week Calculate

List of utility methods to do Week Calculate

Description

The list of methods to do Week Calculate are organized into topic(s).

Method

CalendaralignWeek(Calendar timestamp)
Aligns the time fields to the start of the week.
timestamp = alignDay(timestamp);
int dayOfWeek = timestamp.get(Calendar.DAY_OF_WEEK);
int offset = 1 - dayOfWeek;
if (offset == 0) {
    return timestamp;
return addDays(offset, timestamp);
intdiffWeeks(Date start, Date end)
Number of weeks difference between two dates
if (start == null || end == null) {
    return -1;
return (int) ((end.getTime() - start.getTime()) / (1000 * 60 * 60 * 24 * 7) + 1);
Date[]getBounceDatesOfWeek(Date date)
get Bounce Dates Of Week
Calendar calendar = new GregorianCalendar();
calendar.setFirstDayOfWeek(Calendar.MONDAY);
calendar.setTime(date);
calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
Date begin = calendar.getTime();
calendar.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
Date end = calendar.getTime();
return new Date[] { begin, end };
...
intgetCurrentWeekNum(Date startDate, Date endDate)
get Current Week Num
return 0;
StringgetCurrWeek()
get Curr Week
Calendar c = Calendar.getInstance();
int w = c.get(Calendar.WEEK_OF_YEAR);
if (w < 10) {
    return "0" + w;
return String.valueOf(w);
CalendargetEndOfWeek(Calendar cal)
Moves a calendar to the end of the week
cal.add(Calendar.DAY_OF_WEEK, cal.getActualMaximum(Calendar.DAY_OF_WEEK) - cal.get(Calendar.DAY_OF_WEEK));
return cal;
StringgetSeqWeek()
get Seq Week
Calendar c = Calendar.getInstance(Locale.CHINA);
String week = Integer.toString(c.get(Calendar.WEEK_OF_YEAR));
if (week.length() == 1)
    week = "0" + week;
String year = Integer.toString(c.get(Calendar.YEAR));
return year + week;
intgetWeek(Date date)
get Week
Calendar c = Calendar.getInstance();
c.setTime(date);
int day = c.get(Calendar.DAY_OF_WEEK);
if (day == 1) {
    return 7;
} else {
    return day - 1;
MapgetWeekAbscissa(Class cls)
get Week Abscissa
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MINUTE, 0);
Date end = calendar.getTime();
calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
Date start = calendar.getTime();
Map<Date, T> week = new TreeMap<>();
...
intgetWeekNumberFromDate(Date date)
Compute the week number of parameter date
if (date != null) {
    Calendar calendar = Calendar.getInstance();
    calendar.setFirstDayOfWeek(Calendar.MONDAY);
    calendar.setTime(date);
    return calendar.get(Calendar.WEEK_OF_YEAR);
} else {
    return 0;