简体   繁体   English

获取给定日期的日期以排除两个日期之间的差额

[英]Get the day of the given date to exclude from the difference between two dates

I want to exclude sunday from the differences between two dates. 我想从两个日期之间的差异中排除星期日。 I have used the following code to get the differences between two dates: 我使用以下代码获取两个日期之间的差异:

leaveDuration  = (int)( (leavetodate.getTime() - leavefromdate.getTime()) / (1000 * 60 * 60 * 24) );

Now I want toexclude sunday between the two dates. 现在,我要排除两个日期之间的星期天。 Or for that matter I want to exclude a holiday in general. 或为此,我想排除一般的假期。 How should the above code be modified to get the desired result? 上面的代码应该如何修改以获得期望的结果?

You need to use Calendar 您需要使用日历

int getDiff(Date fromDate, Date toDate) {
    Calendar c = Calendar.getInstance();
    c.setTime(fromDate);
    int workDays = 0;
    while (c.getTime().before(toDate)) {
        if (c.get(Calendar.DAY_OF_WEEK) != Calendar.SUNDAY) {
            workDays++;
        }
        c.add(Calendar.DATE, 1);
    }
    return workDays;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM