检查日期范围与缺失天数的其他日期范围

时间:2013-05-03 06:17:25

标签: date datetime xpages

这是我想要做的: 我有一个日期范围从例如2013年4月3日至2013年4月23日 - 这是我的主要范围。

现在我有可能创建一些自己的时间范围(例如,2013年4月4日至2013年4月4日和2013年4月11日至2013年4月23日)。那些必须涵盖整个主要范围,因此主要范围(不包括周期)的每一天都需要在我自己的时间范围内的相应日期。

我的计划是为主要范围创建一个数组。然后我检查我自己的时间范围的每一天与主要范围。如果有一个符合,我会删除主要范围内的那一天。 所以最后,如果一切正常,就会有一个emtpy数组,因为所有的日子都被我自己的时间范围所覆盖。如果没有,那么未涵盖的日期仍将在主要范围内,我可以与他们合作(在此示例中:03.04.2013,10.04.2013)

有没有人有更好的想法来解决这个问题? NotesDateTimeRanges?

2 个答案:

答案 0 :(得分:2)

我会将日期添加到sorted collection,然后再添加“盗版算法”。向左看,向右看,如果任何外观失败,你可以停止(除非你想找到所有缺失的日期)。

离开我的头(您可能需要按下最终列表以存储值):

var AbsenctSince:NotesDateTime; //Start Date - stored in the NotesItem
var endDate:NotesDateTime; // Return, could be in Notes or Today
var wfDoc:NotesDocument = docApplication.getDocument();
var responseCDs:NotesDocumentCollection = wfDoc.getResponses();
var docResponse:NotesDocument;
var nextResponse:NotesDocument;

//Get the date, which limits the function - if there is a return information, then this is the limit, else today
AbsenctSince = wfDoc.getDateTimeValue("AbsentSince") ;
if (wfDoc.hasItem("ReturnInformationDat")) {
    endDate = wfDoc.getDateTimeValue("ReturnInformationDat");
} else {
    endDate = session.createDateTime("Today");
}

//Get all days between two dates - as pure Java!
var dateList:java.util.List = getWorkDayList(AbsenctSince.toJavaDate(), endDate.toJavaDate());

// Looping once through the reponse documents
var docResponse = responseCDs.getFirstDocument();
while (docResponse != null) {
    nextResponse = responseCDs.getNextDocument(docResponse);
    var CDValidSince:NotesDateTime = docResponse.getDateTimeValue("CDValidSince");
    var CDValidTill:NotesDateTime = docResponse.getDateTimeValue("CDValidTill");
    // Now we need get all days in this range
    var removeDates:java.util.List = getWorkDayList(CDValidSince.toJavaDate(),CDValidTill.toJavaDate());
    dateList.removeAll(removeDates);
    docResponse.recycle();
    docResponse = nextResponse;  
}   
// Both docs are null - nothing to recycle left
// Now we only have uncovered dates left in dateList

docApplication.replaceItemValue("openDates", dateList);

// Cleanup
try {
 AbsenctSince.recycle();
 endDate.recyle();
 wfDoc.recycle();
 responseCDs.recycle(); 
} catch (e) {
dBar.error(e);
}

function getWorkDayList(startDate, endDate) {
var dates:java.util.List = new java.util.ArrayList();
var calendar:java.util.Calendar = new java.util.GregorianCalendar();
    calendar.setTime(startDate);
    while (calendar.getTime().before(endDate)) {
   var workDay = calendar.get(calendar.DAY_OF_WEEK);
   if (workDay != calendar.SATURDAY && workDay != calendar.SUNDAY) {
        var result = calendar.getTime();    
        dates.add(result);
   }
     calendar.add(java.util.Calendar.DATE, 1);
   }
   return dates;   
}

答案 1 :(得分:1)

我现在已经这样做了(到目前为止似乎有效):

var dateArray = new Array();
var responseCDs:NotesDocumentCollection = docApplication.getDocument().getResponses();
var dt:NotesDateTime = session.createDateTime("Today");
var wfDoc = docApplication.getDocument();
dt.setNow();

//Get the date, which limits the function - if there is a return information, then this is the limit, else today
var AbsenctSince:NotesDateTime = session.createDateTime(wfDoc.getItemValue("AbsentSince").toString().substr(0,19));
if (wfDoc.hasItem("ReturnInformationDat")) {
    var endDate:NotesDateTime = session.createDateTime(wfDoc.getItemValue("ReturnInformationDat").toString().substr(0,19));
} else {
    var endDate:NotesDateTime = session.createDateTime("Today");
}

//Get all days between two dates
dateArray = getDates(AbsenctSince, endDate);

for (var i=dateArray.length-1; i >= 0 ; i--) {
    var checkDate:NotesDateTime = session.createDateTime(dateArray[i].toString().substr(0,19));
    var day = checkDate.toJavaDate().getDay();

    //Remove weekends first
    if ((day == 6) || (day == 0)) { //6 = Saturday, 0 = Sunday
        dBar.info("splice: " + dateArray[i]);
        dateArray = dateArray.splice(i,1);
    } else {
        var docResponse = responseCDs.getFirstDocument();
        //Work through all response docs to check if any date is covered
        while (docResponse != null) {
            var CDValidSince:NotesDateTime = session.createDateTime(docResponse.getItemValue("CDValidSince").toString().substr(0,19));
            var CDValidTill:NotesDateTime = session.createDateTime(docResponse.getItemValue("CDValidTill").toString().substr(0,19));

            //checkDate covered? If yes, it will be removed
            if (checkDate.timeDifference(CDValidSince)/86400 >= 0 && checkDate.timeDifference(CDValidTill)/86400 <= 0 ) {
                dBar.info("splice: " + dateArray[i]);
                dateArray = dateArray.splice(i,1);
            }

            docResponse = responseCDs.getNextDocument();
        }
    }   
}

docApplication.replaceItemValue("openDates", dateArray);

我正在使用此功能(从question here采用):

function getDates(startDate:NotesDateTime, endDate:NotesDateTime) {
    var dateArray = new Array();
    var currentDate:NotesDateTime = startDate;
    while (endDate.timeDifference(currentDate) > 0) {
        dateArray.push( currentDate.getDateOnly() );
        currentDate.adjustDay(1);
    }
    return dateArray;
}