我需要找到上个月的最后一个星期五。这个月是六月。我需要五月的最后一个星期五。在这种情况下(5月29日)。我只能找到本月的最后一个星期五。找到上个月的星期五后,我需要检查已经过去多少天了。如果自上一个星期五以来已经过5天,则执行任务。希望这很清楚。如果没有,请询​​问,我可以详细解释。

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

最佳答案

您快到了,只需将以下行添加到您的getLastFriday方法中:

// 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;
}

然后,您可以阅读以下问题之一及其答案,以获取天数差异:Finding days difference in javaCalculating the difference between two Java date instances

09-26 04:08