久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

如何使用 Java 日歷忽略周末?

How do I ignore weekends using the Java calendar?(如何使用 Java 日歷忽略周末?)
本文介紹了如何使用 Java 日歷忽略周末?的處理方法,對(duì)大家解決問(wèn)題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧!

問(wèn)題描述

限時(shí)送ChatGPT賬號(hào)..

我試圖通過(guò)忽略周末來(lái)獲取兩個(gè)特定時(shí)間實(shí)例之間的分鐘數(shù).這就是我所做的.

I'm trying to get the number of minutes between two particular time instances by ignoring weekends. This is what I've done.

public static final List<Integer> NON_WORKING_DAYS;
static {
    List<Integer> nonWorkingDays = new ArrayList<Integer>();
    nonWorkingDays.add(Calendar.SATURDAY);
    nonWorkingDays.add(Calendar.SUNDAY);
    NON_WORKING_DAYS = Collections.unmodifiableList(nonWorkingDays);
}

public static int getMinsBetween(Date d1, Date d2, boolean onlyBusinessDays)
{
    int minsBetween = (int)((d2.getTime() - d1.getTime()) / (1000 * 60));
    int minsToSubtract = 0;
    if(onlyBusinessDays){
        Calendar dateToCheck = Calendar.getInstance();
        dateToCheck.setTime(d1);
        Calendar dateToCompare = Calendar.getInstance();
        dateToCompare.setTime(d2);


        //moving the first day of the week to Tues so that a Sat, sun and mon fall in the same week, easy to adjust dates
        dateToCheck.setFirstDayOfWeek(Calendar.TUESDAY);
        dateToCompare.setFirstDayOfWeek(Calendar.TUESDAY);

        //moving the dates out of weekends
        if(!isBusinessDay(dateToCheck, NON_WORKING_DAYS)){
            dateToCheck.set(Calendar.DAY_OF_WEEK, Calendar.SATURDAY);
            dateToCheck.set(Calendar.HOUR, 0);
            dateToCheck.set(Calendar.MINUTE, 0);
            dateToCheck.set(Calendar.SECOND, 0);
            dateToCheck.set(Calendar.MILLISECOND, 0);
        }

        if(!isBusinessDay(dateToCompare, NON_WORKING_DAYS)){
            dateToCompare.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
            dateToCompare.set(Calendar.HOUR, 0);
            dateToCompare.set(Calendar.MINUTE, 0);
            dateToCompare.set(Calendar.SECOND, 0);
            dateToCompare.set(Calendar.MILLISECOND, 0);
        }

        for(; dateToCheck.getTimeInMillis() < dateToCompare.getTimeInMillis() ; dateToCheck.add(Calendar.DAY_OF_MONTH, 1)){
            if(isBusinessDay(dateToCheck, NON_WORKING_DAYS)){
                minsToSubtract = minsToSubtract + 1440;
            }
        }
    minsBetween = minsBetween - minsToSubtract; 
    }
    return minsBetween;
}


private static boolean isBusinessDay(Calendar dateToCheck, List<Integer> daysToExclude){
    for(Integer dayToExclude : daysToExclude){
        if(dayToExclude != null && dayToExclude == dateToCheck.get(Calendar.DAY_OF_WEEK)) {
            return true; 
        }
        else continue;
    }
    return false;
}

誰(shuí)能告訴我我的邏輯是否正確,如果不正確,該怎么做?我不太確定當(dāng)周末月份發(fā)生變化時(shí)這段代碼會(huì)如何表現(xiàn).

Can someone tell me if my logic is correct and if not how to do this? I'm not too sure how this code would behave when the month changes over the weekend.

一些測(cè)試用例的預(yù)期輸出:

Expected output for some test cases:

  1. 周五下午 6 點(diǎn),周一早上 6 點(diǎn) - 應(yīng)該返回 12 小時(shí)
  2. 周六 12 點(diǎn),周日 12 點(diǎn) - 應(yīng)該返回 0 小時(shí)
  3. 星期六中午 12 點(diǎn),星期一早上 6 點(diǎn) - 應(yīng)該返回 6 小時(shí)

推薦答案

我強(qiáng)烈推薦使用 Joda-時(shí)間在Java中處理有關(guān)日期的任何事情,因?yàn)樗鼛в性S多有用的功能,可以使代碼不那么復(fù)雜.

I would highly recommend using Joda-Time for anything concerning date manipulations in Java, because it comes with a lot of helpful functions to make the code less complicated.

此代碼使用 JodaTime:

This code uses JodaTime:

public static final List<Integer> NON_WORKING_DAYS;
static {
    List<Integer> nonWorkingDays = new ArrayList<Integer>();
    nonWorkingDays.add(DateTimeConstants.SATURDAY);
    nonWorkingDays.add(DateTimeConstants.SUNDAY);
    NON_WORKING_DAYS = Collections.unmodifiableList(nonWorkingDays);
}

public static Minutes getMinsBetween(DateTime d1, DateTime d2,
        boolean onlyBusinessDays) {

    BaseDateTime startDate = onlyBusinessDays && !isBusinessDay(d1) ?
                new DateMidnight(d1) : d1;
    BaseDateTime endDate = onlyBusinessDays && !isBusinessDay(d2) ?
                new DateMidnight(d2) : d2;

    Minutes minutes = Minutes.minutesBetween(startDate, endDate);

    if (onlyBusinessDays) {
        DateTime d = new DateTime(startDate);

        while (d.isBefore(endDate)) {
            if (!isBusinessDay(d)) {
                Duration dayDuration = new Duration(d, d.plusDays(1));
                minutes = minutes.minus(int) dayDuration.getStandardMinutes());
            }
            d = d.plusDays(1);
        }
    }
    return minutes;
}

private static boolean isBusinessDay(DateTime dateToCheck) {
    return !NON_WORKING_DAYS.contains(dateToCheck.dayOfWeek().get());
}

當(dāng)測(cè)試這段代碼時(shí),它給出了以下結(jié)果:

When this code is tested, it gives the following results:

DateTime d1 = new DateTime(2013, 1, 4, 18, 0); // a Friday, 6 pm
DateTime d2 = new DateTime(2013, 1, 7, 6, 0);  // the following Monday, 6 am

Minutes minutes = getMinsBetween(d1, d2, true);
System.out.println(minutes.toStandardHours().getHours()); // outputs "12" (in hours)

d1 = new DateTime(2013, 1, 5, 12, 0); // a Saturday, 12 pm
d2 = new DateTime(2013, 1, 6, 12, 0); // the following Sunday, 12 pm

minutes = getMinsBetween(d1, d2, true);
System.out.println(minutes.toStandardHours().getHours()); // outputs "0" (in hours)

d1 = new DateTime(2013, 1, 5, 12, 0); // a Saturday, 12 pm
d2 = new DateTime(2013, 1, 7, 6, 0);  // the following Monday, 6 am

minutes = getMinsBetween(d1, d2, true);
System.out.println(minutes.toStandardHours().getHours()); // outputs "6" (in hours)

我剛剛測(cè)試了一個(gè)周末換月的案例:從 3 月 29 日星期五(下午 6 點(diǎn))到 4 月 1 日星期一(早上 6 點(diǎn)):

I just tested a case where the month changes over the weekend: From Friday, March 29th (6pm) to Monday, April 1st (6am):

d1 = new DateTime(2013, 3, 29, 18, 0);
d2 = new DateTime(2013, 4, 1, 6, 0);

minutes = getMinsBetween(d1, d2, true);
System.out.println(minutes.toStandardHours().getHours());

結(jié)果是 12 小時(shí),所以它適用于月份變化.

The result is 12 hours, so it works for the month change.

我的第一個(gè)解決方案沒(méi)有正確處理夏令時(shí).我們必須在減去分鐘時(shí)確定每個(gè)實(shí)際天的持續(xù)時(shí)間,因?yàn)橄牧顣r(shí)更改的天數(shù)不會(huì)是 24 小時(shí):

My first solution wasn't handling daylight saving times correctly. We have to determine the duration of each actual day when subtracting the minutes because days with a change in daylight saving time will not be 24h:

if (!isBusinessDay(d)) {
    Duration dayDuration = new Duration(d, d.plusDays(1));
    minutes = minutes.minus(int) dayDuration.getStandardMinutes());
}

這篇關(guān)于如何使用 Java 日歷忽略周末?的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來(lái)源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問(wèn)題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

Parsing an ISO 8601 string local date-time as if in UTC(解析 ISO 8601 字符串本地日期時(shí)間,就像在 UTC 中一樣)
How to convert Gregorian string to Gregorian Calendar?(如何將公歷字符串轉(zhuǎn)換為公歷?)
Java: What/where are the maximum and minimum values of a GregorianCalendar?(Java:GregorianCalendar 的最大值和最小值是什么/在哪里?)
Calendar to Date conversion for dates before 15 Oct 1582. Gregorian to Julian calendar switch(1582 年 10 月 15 日之前日期的日歷到日期轉(zhuǎn)換.公歷到儒略歷切換)
java Calendar setFirstDayOfWeek not working(java日歷setFirstDayOfWeek不起作用)
Java: getting current Day of the Week value(Java:獲取當(dāng)前星期幾的值)
主站蜘蛛池模板: 色综合视频| 欧美国产视频 | 国产视频999 | 欧美一区二区精品 | 日韩高清一区二区 | 97中文字幕 | 欧美日韩啪啪 | 俺去俺来也在线www色官网 | 日本中文字幕在线观看 | 亚洲精品91 | 诱人的大乳奶3做爰 | 国产精品欧美在线 | 亚洲第一免费视频 | 亚洲一区国产精品 | 黄色一级视频 | 欧美三级又粗又硬 | 亚洲网站在线 | 成人午夜在线观看 | 亚洲免费一区二区 | www.99精品| 欧美日韩一区二区三区四区 | 欧美日韩中文在线 | 亚洲一区二区在线播放 | 国产黄色免费看 | 成人av播放| 日本a在线观看 | 四虎在线免费观看视频 | 精品一区二区免费视频 | 亚洲欧美另类在线 | 日韩伦理在线观看 | 天天操夜夜骑 | 色中色综合 | 亚洲视频三区 | 国产精品网站在线观看 | 精品国产三级 | 久久神马 | 成人在线播放视频 | 青青草一区二区 | 视频在线一区二区 | 国产免费高清 | 免费在线成人网 |