I am working on a project where the requirement is to have a date calculated as being the last Friday of a given month. I think I have a solution that only uses standard Ja
Below program is for the last Friday of each month. it can be used to get the last of any day of the week in any month. The variable offset=0
means current month(system date), offset=1 means next month, so on. The getLastFridayofMonth(int offset)
method will return the last Friday.
import java.text.SimpleDateFormat;
import java.util.Calendar;
public class LastFriday {
public static Calendar getLastFriday(Calendar cal,int offset){
int dayofweek;//1-Sunday,2-Monday so on....
cal.set(Calendar.MONTH,cal.get(Calendar.MONTH)+offset);
cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH)); //set calendar to last day of month
dayofweek=cal.get(Calendar.DAY_OF_WEEK); //get the day of the week for last day of month set above,1-sunday,2-monday etc
if(dayofweek<Calendar.FRIDAY) //Calendar.FRIDAY will return integer value =5
cal.set(Calendar.DAY_OF_MONTH, cal.get(Calendar.DAY_OF_MONTH)-7+Calendar.FRIDAY-dayofweek);
else
cal.set(Calendar.DAY_OF_MONTH, cal.get(Calendar.DAY_OF_MONTH)+Calendar.FRIDAY-dayofweek);
return cal;
}
public static String getLastFridayofMonth(int offset) { //offset=0 mean current month
final String DATE_FORMAT_NOW = "dd-MMM-yyyy";
Calendar cal = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_NOW);
cal=getLastFriday(cal,offset);
return sdf.format(cal.getTime());
}
public static void main(String[] args) {
System.out.println(getLastFridayofMonth(0)); //0 = current month
System.out.println(getLastFridayofMonth(1));//1=next month
System.out.println(getLastFridayofMonth(2));//2=month after next month
}
}
Though I agree with scubabbl, here is a version without an inner while.
int year = 2008;
for (int m = Calendar.JANUARY; m <= Calendar.DECEMBER; m++) {
Calendar cal = new GregorianCalendar(year, m, 1);
cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));
int diff = Calendar.FRIDAY - cal.get(Calendar.DAY_OF_WEEK);
if (diff > 0) {
diff -= 7;
}
cal.add(Calendar.DAY_OF_MONTH, diff);
System.out.println(cal.getTime());
}
Let Calendar.class do its magic for you ;)
pCal.set(GregorianCalendar.DAY_OF_WEEK,Calendar.FRIDAY);
pCal.set(GregorianCalendar.DAY_OF_WEEK_IN_MONTH, -1);
public static int lastSundayDate()
{
Calendar cal = getCalendarInstance();
cal.setTime(new Date(getUTCTimeMillis()));
cal.set( Calendar.DAY_OF_MONTH , 25 );
return (25 + 8 - (cal.get(Calendar.DAY_OF_WEEK) != Calendar.SUNDAY ? cal.get(Calendar.DAY_OF_WEEK) : 8));
}
Based on marked23's suggestion:
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();
}
You never need to loop to find this out. For determining the "last Friday" date for this month, start with the first day of next month. Subtract the appropriate number of days depending on what (numerical) day of the week the first day of the month falls on. There's your "last Friday." I'm pretty sure it can be boiled down to a longish one-liner, but I'm not a java dev. So I'll leave that to someone else.