Getting Last Thursday of given month,year

public int getLastThursday(int month, int year)
{
Calendar cal = Calendar.getInstance();
   cal.set(year, month, 1, 0, 0, 0);
   cal.set(Calendar.MILLISECOND, 0);
   int daysOfMonth = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
   int firstDay = cal.get(Calendar.DAY_OF_WEEK)-1;
 
int forword[]={4,3,2,1,0,6,7};
int backword[]={3,4,5,6,0,1,2};
int daysforword[] = new int[9];
for(int i=0;i<forword.length;i++)
{
daysforword[i]=29+forword[i];
}

if(daysforword[firstDay]>daysOfMonth)
{
return (29-backword[firstDay]);
}
else if(daysforword[firstDay]==daysOfMonth)
{
return daysOfMonth;
}
else if(daysforword[firstDay]<daysOfMonth)
{
return (29+forword[firstDay]);
}


return 0;

}

————————————————————————————
logic
—————————

28,0,,,sunday,1,7,4,3,33,26,,5,4
29,1,,,monday,2,6,3,4,32,25,,4,5
30,2,,,Tuesday,3,5,2,5,31,31,24,3,6
31,3,,,Wednesday,4,4,1,6,30,30,23,2,7
,,,,Thursday,5,3,0,7,29,29,22,1,1
,,,,Friday,6,2,6,1,35,28,,7,2
,,,,Saturday,7,1,7,2,36,27,,6,3
,,,,,,,forword diff b/w start days and wedensday,,,,,,
,,,,,,,,backword diff b/w start days and wedensday,,,,,
,,,,,,,,,,,,,
,,,,,,,,,29,,,,
,,,,,,,,,29+forword,,,,
,,,,,,,,,,,,,
,,,,,,,,,,,,,
,,,,,,,,,,,29-backword  if <=31 to>=29,,

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: