gpt4 book ai didi

java - 查找上个月的最后一个星期五并检查当前日期之间的天数

转载 作者:行者123 更新时间:2023-11-30 08:51:16 25 4
gpt4 key购买 nike

我需要找到上个月的最后一个星期五。这个月是六月。我需要五月的最后一个星期五。在这种情况下(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;
}
}

最佳答案

查找last Friday of any month使用方法 -

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

您可以使用以下方法找出 2 天之间的差异 -

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

关于java - 查找上个月的最后一个星期五并检查当前日期之间的天数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30627889/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com