Finding the second friday of last month

Status
Not open for further replies.

mod-the-pc

Back to School Mr. Bean !
I came up with this simple way
Code:
import java.util.*;
class Friday
{
 public static void main(String a[])
 {
  int nthWeek =2, monthsBehind=1;
  int weekDay=6; //Sunday=1, Monday=2, ...Friday=6, Saturday=7
  System.out.println(getNthWeekDay(monthsBehind, nthWeek,weekDay));
 }
 static Date getNthWeekDay(int monthsBehind, int nthWeek, int weekDay)
 {
  GregorianCalendar xMonth=new GregorianCalendar();
  //point calendar to n months behind
  xMonth.add(Calendar.MONTH,-(monthsBehind));
  //select the second week of the month
  xMonth.set(Calendar.WEEK_OF_MONTH,nthWeek);
  //select the required day of the week
  xMonth.set(Calendar.DAY_OF_WEEK,weekDay);
  return xMonth.getTime();
 }
}
 
Last edited:
OP
whoopy_whale

whoopy_whale

Journeyman
mod-the-pc said:
I came up with this simple way
Code:
import java.util.*;
class Friday
{
 public static void main(String a[])
 {
  int nthWeek =2, monthsBehind=1;
  int weekDay=6; //Sunday=1, Monday=2, ...Friday=6, Saturday=7
  System.out.println(getNthWeekDay(monthsBehind, nthWeek,weekDay));
 }
 static Date getNthWeekDay(int monthsBehind, int nthWeek, int weekDay)
 {
  GregorianCalendar xMonth=new GregorianCalendar();
  //point calendar to n months behind
  xMonth.add(Calendar.MONTH,-(monthsBehind));
  //select the second week of the month
  xMonth.set(Calendar.WEEK_OF_MONTH,nthWeek);
  //select the required day of the week
  xMonth.set(Calendar.DAY_OF_WEEK,weekDay);
  return xMonth.getTime();
 }
}


Thanks buddy... :)
 
Status
Not open for further replies.
Top Bottom