Here you can find the source of getBothDayOfWeek(String str, int week)
public static String[] getBothDayOfWeek(String str, int week) throws ParseException
//package com.java2s; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; public class Main { public static String[] getBothDayOfWeek(String str, int week) throws ParseException { return new String[] { getFirstDayOfWeek(str, week), getLastDayOfWeek(str, week) }; }/*from ww w. ja va 2s.co m*/ public static String[] getBothDayOfWeek(Date date, int week) throws ParseException { return new String[] { getFirstDayOfWeek(date, week), getLastDayOfWeek(date, week) }; } public static String getFirstDayOfWeek(String str, int week) throws ParseException { String conStr = null; int dayOfWeek = 0; if (week == 0) { conStr = str; dayOfWeek = getCalendar(conStr).get(Calendar.DAY_OF_WEEK); } else { conStr = addDays(str, week * 7); dayOfWeek = getCalendar(conStr).get(Calendar.DAY_OF_WEEK); } int gap = 0; if (dayOfWeek != 1) gap = dayOfWeek - 2; else gap = 6; return addDays(conStr, -gap); } public static String getFirstDayOfWeek(Date date, int week) throws ParseException { Date conDate = null; int dayOfWeek = 0; if (week == 0) { conDate = date; dayOfWeek = getCalendar(conDate).get(Calendar.DAY_OF_WEEK); } else { conDate = addDays2Date(date, week * 7); dayOfWeek = getCalendar(conDate).get(Calendar.DAY_OF_WEEK); } int gap = 0; if (dayOfWeek != 1) gap = dayOfWeek - 2; else gap = 6; return addDays(conDate, -gap); } public static String getLastDayOfWeek(String str, int week) throws ParseException { String conStr = null; int dayOfWeek = 0; if (week == 0) { conStr = str; dayOfWeek = getCalendar(conStr).get(Calendar.DAY_OF_WEEK); } else { conStr = addDays(str, week * 7); dayOfWeek = getCalendar(conStr).get(Calendar.DAY_OF_WEEK); } int gap = 0; if (dayOfWeek != 1) gap = 8 - dayOfWeek; else gap = 0; return addDays(conStr, gap); } public static String getLastDayOfWeek(Date date, int week) throws ParseException { Date conDate = null; int dayOfWeek = 0; if (week == 0) { conDate = date; dayOfWeek = getCalendar(conDate).get(Calendar.DAY_OF_WEEK); } else { conDate = addDays2Date(date, week * 7); dayOfWeek = getCalendar(conDate).get(Calendar.DAY_OF_WEEK); } int gap = 0; if (dayOfWeek != 1) gap = 8 - dayOfWeek; else gap = 0; return addDays(conDate, gap); } public static Calendar getCalendar(String str) { int yy = Integer.parseInt(str.substring(0, 4)); int mm = Integer.parseInt(str.substring(4, 6)) - 1; int dd = Integer.parseInt(str.substring(6, 8)); Calendar cal = Calendar.getInstance(); cal.set(yy, mm, dd); return cal; } public static Calendar getCalendar(Date date) { Calendar cal = Calendar.getInstance(); cal.setTime(date); return cal; } public static String addDays(String str, int days) throws ParseException { SimpleDateFormat fmt = new SimpleDateFormat("yyyyMMdd"); Date date = fmt.parse(str); date.setTime(date.getTime() + (long) days * 1000L * 60L * 60L * 24L); return fmt.format(date); } public static String addDays(Date date, int days) throws ParseException { SimpleDateFormat fmt = new SimpleDateFormat("yyyyMMdd"); date.setTime(date.getTime() + (long) days * 1000L * 60L * 60L * 24L); return fmt.format(date); } public static Date addDays2Date(String str, int days) throws ParseException { SimpleDateFormat fmt = new SimpleDateFormat("yyyyMMdd"); Date date = fmt.parse(str); date.setTime(date.getTime() + (long) days * 1000L * 60L * 60L * 24L); return date; } public static Date addDays2Date(Date date, int days) throws ParseException { date.setTime(date.getTime() + (long) days * 1000L * 60L * 60L * 24L); return date; } }