第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

計(jì)算Java中兩個(gè)日期之間的工作日數(shù)

計(jì)算Java中兩個(gè)日期之間的工作日數(shù)

手掌心 2019-09-24 10:52:07
誰能指出我一些Java摘錄,讓我可以在兩個(gè)日期之間進(jìn)行商務(wù)活動(dòng)(星期六和星期日除外)。
查看完整描述

3 回答

?
www說

TA貢獻(xiàn)1775條經(jīng)驗(yàn) 獲得超8個(gè)贊

5行代碼無循環(huán)的解決方案

定義之間的天數(shù)的方式與ChronoUnit.DAYS.between(start, end)表示4星期一至星期五之間存在天數(shù)的方式相同。由于我們只對(duì)工作日感興趣,因此我們必須減去周末,因此從星期五到星期二會(huì)有2工作日(只需計(jì)算endDay - startDay并減去2周末)。1如果要包含結(jié)果,則添加到結(jié)果中,即不要間隔幾天。


我提出兩種解決方案。


第一個(gè)解決方案(5線,簡(jiǎn)短和隱秘):


import java.time.*;

import java.time.temporal.*;


public static long calcWeekDays1(final LocalDate start, final LocalDate end) {

    final DayOfWeek startW = start.getDayOfWeek();

    final DayOfWeek endW = end.getDayOfWeek();


    final long days = ChronoUnit.DAYS.between(start, end);

    final long daysWithoutWeekends = days - 2 * ((days + startW.getValue())/7);


    //adjust for starting and ending on a Sunday:

    return daysWithoutWeekends + (startW == DayOfWeek.SUNDAY ? 1 : 0) + (endW == DayOfWeek.SUNDAY ? 1 : 0);

}

第二種解決方案:


public static long calcWeekDays2(final LocalDate start, final LocalDate end) {

    final int startW = start.getDayOfWeek().getValue();

    final int endW = end.getDayOfWeek().getValue();


    final long days = ChronoUnit.DAYS.between(start, end);

    long result = days - 2*(days/7); //remove weekends


    if (days % 7 != 0) { //deal with the rest days

        if (startW == 7) {

            result -= 1;

        } else if (endW == 7) {  //they can't both be Sunday, otherwise rest would be zero

            result -= 1;

        } else if (endW < startW) { //another weekend is included

            result -= 2;

        }

    }


    return result;

}


查看完整回答
反對(duì) 回復(fù) 2019-09-24
  • 3 回答
  • 0 關(guān)注
  • 1832 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)