0

I found this method here:

public Date getLastFriday( int month, int year ) {
    Calendar cal = Calendar.getInstance();
    cal.set( year, month + 1, 1 );
    cal.add( Calendar.DAY_OF_MONTH, -(cal.get( Calendar.DAY_OF_WEEK ) % 7 + 1 ));
    return cal.getTime();
} 

I want similar functionality like this:

private Date getFirstThursday(Date now) {
    Calendar cal = Calendar.getInstance();
    cal.setTime(now);
    cal.add(Calendar.DAY_OF_MONTH, ???);
    return cal.getTime();
}

Can anyone help what I should replace? Marked with.

icza
  • 389,944
  • 63
  • 907
  • 827
  • Have you referred to these questions? http://stackoverflow.com/questions/21265315/find-out-the-date-for-first-sunday-monday-etc-of-the-month http://stackoverflow.com/questions/23971439/get-the-first-monday-of-a-month – Pramod Karandikar Nov 11 '14 at 08:15
  • Merhaba Emek. Try joda-time library, it's much better for this sort of thing. – vikingsteve Nov 11 '14 at 08:19
  • 1
    If you already have `getLastFriday()` implementation, then to create `getFirstThursday()` all you need is _effort_ and thinking from your part, nothing more. Looks you haven't put any into solving your problem. – icza Nov 11 '14 at 08:22

1 Answers1

0

Try this code:

private static Date getFirstThursday(Date now) {
  Calendar cal = Calendar.getInstance();
  cal.setTime(now);
  cal.set(Calendar.WEEK_OF_MONTH, 1);
  cal.set(Calendar.DAY_OF_WEEK, Calendar.THURSDAY);

  return cal.getTime();
}
Jens
  • 67,715
  • 15
  • 98
  • 113