Here you can find the source of getADayOfWeek(Calendar day, int dayOfWeek)
private static Calendar getADayOfWeek(Calendar day, int dayOfWeek)
//package com.java2s; //License from project: Apache License import java.util.Calendar; public class Main { private static Calendar getADayOfWeek(Calendar day, int dayOfWeek) { int week = day.get(Calendar.DAY_OF_WEEK); if (week == dayOfWeek) return day; int diffDay = dayOfWeek - week; if (week == Calendar.SUNDAY) { diffDay -= 7;/*from w w w . j a v a 2 s. c o m*/ } else if (dayOfWeek == Calendar.SUNDAY) { diffDay += 7; } day.add(Calendar.DATE, diffDay); return day; } }