Java Utililty Methods Week

List of utility methods to do Week

Description

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

Method

StringcurrentWeekEndDate()
current Week End Date
Calendar mth = Calendar.getInstance();
mth.set(mth.get(Calendar.YEAR), mth.get(Calendar.MONTH),
        mth.get(Calendar.DAY_OF_MONTH) - (mth.get(Calendar.DAY_OF_WEEK) + 6) % 7 + 7);
return (new SimpleDateFormat("yyyy-MM-dd").format(mth.getTime()));
StringgetNextWeek(String week)
get Next Week
return getDeltaWeek(week, 1);
StringgetPreWeek(String week)
get Pre Week
return getDeltaWeek(week, -1);
StringgetPreWeekDayByStr(String curday)
get Pre Week Day By Str
try {
    java.util.Date date = str2utilDate(curday);
    Calendar rightNow = Calendar.getInstance();
    rightNow.setTime(date);
    int week = rightNow.get(Calendar.DAY_OF_WEEK);
    if (week == 0)
        week = 1;
    if (week == 7)
...
StringgetPreWeekDayByStr(String curday)
get Pre Week Day By Str
try {
    java.util.Date date = str2date(curday);
    return getDateStr(new java.util.Date(date.getTime() - 7 * 24 * 3600 * 1000));
} catch (Exception e) {
    e.printStackTrace();
    return "";
SortedMapgetPreWeeks(int weekNum)

Description:Get the start date - end date string before current week according to week number

specified by parameter The format is "yyyy/MM/dd-yyyy/MM/dd"
SortedMap<Integer, String> m = new TreeMap<Integer, String>();
Calendar now = Calendar.getInstance();
now.add(Calendar.DAY_OF_YEAR, -(7 * (weekNum - 1)));
SimpleDateFormat formatter = new SimpleDateFormat("yyyy/MM/dd", new DateFormatSymbols(Locale.US));
int day = now.get(Calendar.DAY_OF_WEEK);
int offset = offsets[day - Calendar.SUNDAY] - 1;
now.add(Calendar.DAY_OF_YEAR, offset);
String temp = null;
...
StringgetStartDate(Date date, int weeks, Locale locale)
This method receives the @params Date date,int weeks,Locale locale It sets a Gregorian Calendar with the given Date, with the given locale a format style is determined.
Calendar cal_date = new GregorianCalendar();
cal_date.setTime(date);
DateFormat df = DateFormat.getDateInstance(DateFormat.FULL, locale);
int startWeek = getWeek(weeks);
cal_date.set(Calendar.WEEK_OF_YEAR, startWeek);
String strstart = df.format(cal_date.getTime());
strstart = getStartDateOfWeek(cal_date.getTime(), locale);
return strstart;
...
StringgetStartDateOfWeek(Date selectedDate, Locale locale)
This method sets a Gregorian Calendar according to the given Date
DateFormat df = DateFormat.getDateInstance(DateFormat.FULL, locale);
Calendar cal_date = new GregorianCalendar();
cal_date.setTime(selectedDate);
int strDay = cal_date.get(Calendar.DAY_OF_WEEK);
cal_date.add(Calendar.DATE, -strDay + 1);
return format(cal_date.getTime(), Locale.getDefault());
StringgetThisWeek()

Description:Get the start date - end date string before current week according to week number

specified by parameter The format is "yyyy/MM/dd-yyyy/MM/dd"
Calendar now = Calendar.getInstance();
SimpleDateFormat formatter = new SimpleDateFormat("yyyy/MM/dd", new DateFormatSymbols(Locale.US));
int day = now.get(Calendar.DAY_OF_WEEK);
int offset = offsets[day - Calendar.SUNDAY] - 1;
now.add(Calendar.DAY_OF_YEAR, offset);
String temp = null;
now.add(Calendar.DAY_OF_YEAR, +1);
temp = formatter.format(now.getTime());
...
ListgetThisWeek()
get This Week
return getThisWeek(DEFAULT_DATE_FORMAT);