Here you can find the source of getDayAfterGivenDate(Date date, int dayOfWeek)
Parameter | Description |
---|---|
date | The date after which the date we compute should fall |
dayOfWeek | The day of week on which the date we compute should fall (Use Calendar.SUNDAY etc.) |
public static Date getDayAfterGivenDate(Date date, int dayOfWeek)
//package com.java2s; // distribute, sublicense, and/or sell copies of the Software, and to import java.util.Calendar; import java.util.Date; public class Main { /**/*from w w w . j av a 2 s .c o m*/ * Find the given day of the week (sunday, monday etc.) that falls before * the given date * * @param date * The date after which the date we compute should fall * @param dayOfWeek * The day of week on which the date we compute should fall (Use * Calendar.SUNDAY etc.) * @return */ public static Date getDayAfterGivenDate(Date date, int dayOfWeek) { // First find what day of week the given date falls Calendar cal = Calendar.getInstance(); cal.setTime(date); int originalDayOfWeek = cal.get(Calendar.DAY_OF_WEEK); int dayDiff = dayOfWeek - originalDayOfWeek; if (dayDiff <= 0) { dayDiff += 7; } cal.add(Calendar.DATE, dayDiff); return cal.getTime(); } }