Java Utililty Methods Week Day

List of utility methods to do Week Day

Description

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

Method

longgetLastWeekMs()
get Last Week Ms
return new Date().getTime() - 7 * 24 * 60 * 60 * 1000;
StringgetMon()
get Mon
Date date = Calendar.getInstance().getTime();
String sDate = convDateToString(date, "MM");
return sDate;
StringgetMonday(String date)
get Monday
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
SimpleDateFormat format2 = new SimpleDateFormat("yyyyMMdd");
Date d = null;
try {
    d = format.parse(date);
} catch (Exception e) {
    e.printStackTrace();
Calendar cal = Calendar.getInstance();
cal.setTime(d);
int days = cal.get(Calendar.DAY_OF_WEEK);
if (days == 1) {
    days = 1;
    String preMonday = getPreviousMonday(d, days);
    System.out.println("preMonday" + preMonday);
    return preMonday;
} else {
    cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
    return format2.format(cal.getTime());
DategetMonday(String date, int weekDay)
get Monday
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
Date d = null;
try {
    d = format.parse(date);
} catch (Exception e) {
    e.printStackTrace();
Calendar cal = Calendar.getInstance();
...
DategetMondayAfter(Date date)
get Monday After
Calendar cal = Calendar.getInstance();
cal.setTime(date);
while (cal.get(Calendar.DAY_OF_WEEK) != Calendar.MONDAY) {
    cal.add(Calendar.DATE, 1);
return cal.getTime();
StringgetMondayOfThisWeek()
get Monday Of This Week
Calendar c = Calendar.getInstance();
int day_of_week = c.get(Calendar.DAY_OF_WEEK) - 1;
if (day_of_week == 0)
    day_of_week = 7;
c.add(Calendar.DATE, -day_of_week + 1);
return new SimpleDateFormat("yyyy-MM-dd").format(c.getTime());
intgetNextDayOfweek()
get Next Day Ofweek
Calendar calendar = Calendar.getInstance();
calendar.setTime(new java.util.Date(date.getTime() + 24 * 60 * 60 * 1000));
if (calendar.get(Calendar.DAY_OF_WEEK) == 1)
    return 7;
else
    return calendar.get(Calendar.DAY_OF_WEEK) - 1;
StringgetPreDay()
get Pre Day
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.DAY_OF_WEEK, -1);
Date preTime = calendar.getTime();
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
return formatter.format(preTime);
longgetPreviousDay(long date, int startOfWeek)
Returns the first day before date that has the day of week matching startOfWeek.
return getDay(date, startOfWeek, -1);
StringgetPreviousFriday(String date)
get Previous Friday
Date d = strToDtSimpleFormat(date);
Calendar cal = Calendar.getInstance();
cal.setTime(d);
cal.add(Calendar.WEEK_OF_YEAR, -1);
cal.set(Calendar.DAY_OF_WEEK, Calendar.FRIDAY);
return dateFormat(cal.getTime());