2

I need to find the Last Friday of the previous month. This month is June. I need the last Friday of May. In this case (May 29th). I am able to only find the Last Friday of current month. After I find the previous month's Friday, I need to check from how many days has it been. If it has been 5 days since the last Friday, then execute a task. Hopefully this is clear. If not, please ask and I can explain in greater detail.

public class task {

static String lastFriday;
static String dtToday;

public static void main(String[] args) {
   //daysInBetween = current day - (last month's friday date)

   if (daysInBetween = 5) {
      //run program after 5 days
   } else { //quit program }
}

// Gets last Friday of the Month
// Need last Friday of previous Month...
public static String getLastFriday() {
    Calendar cal = new GregorianCalendar();
    cal.set(GregorianCalendar.DAY_OF_WEEK, Calendar.FRIDAY);
    cal.set(GregorianCalendar.DAY_OF_WEEK_IN_MONTH, -1);        
    SimpleDateFormat date_format = new SimpleDateFormat("yyyy/MM/dd");
    lastFriday = date_format.format(cal.getTime()); 
    return lastFriday;
}

// Gets today's date
public static String getToday() {
    Calendar cal = new GregorianCalendar();     
    SimpleDateFormat date_format = new SimpleDateFormat("yyyy/MM/dd");      
    dtToday = date_format.format(cal.getTime());    
    return dtToday;
}
}
Razib
  • 10,965
  • 11
  • 53
  • 80
bernardo
  • 25
  • 5

2 Answers2

2

Find last Friday of any month using the method -

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();
}

And you may use the following method to find the diffrence between 2 days -

public int getDifferenceDays(Date d1, Date d2) {
   int daysdiff=0;

   long diff = d2.getTime() - d1.getTime();
   long diffDays = diff / (24 * 60 * 60 * 1000)+1;
   daysdiff = (int) diffDays;

   return daysdiff;
 }
Community
  • 1
  • 1
Razib
  • 10,965
  • 11
  • 53
  • 80
0

You was nearly there, just add the follwing line to your getLastFriday method:

// Gets last Friday of the Month
// Need last Friday of previous Month...
public static String getLastFriday() {
    Calendar cal = new GregorianCalendar();
    // reduce the "current" month by 1 to get the "previous" month
    cal.set(GregorianCalendar.MONTH, cal.get(GregorianCalendar.MONTH) - 1); 
    cal.set(GregorianCalendar.DAY_OF_WEEK, Calendar.FRIDAY);
    cal.set(GregorianCalendar.DAY_OF_WEEK_IN_MONTH, -1);        
    SimpleDateFormat date_format = new SimpleDateFormat("yyyy/MM/dd");
    lastFriday = date_format.format(cal.getTime()); 
    return lastFriday;
}

Then you can read one of these question and their answers to get the difference in days: Finding days difference in java or Calculating the difference between two Java date instances.

Community
  • 1
  • 1
Tom
  • 16,842
  • 17
  • 45
  • 54