Calculate date difference between overlapping periods

hello folks,
i am learning my way into more complex problem of java.
i have to calculate the true period lenght for a set of periods.
it is a little complidated to explain the whole business rules on this subject, but on a nutshell:
we have teams that are give a single day payment for staying in a hotel, but since this team is composed of many people (?this is a team); there are days when more then one person stays in a hotel.
we get these details at the end of a business event when all information is entered on our business intelligence system.
for a given hotel stay, the following details are submitted:
start date
end date
number of days in economic accomodation (lower class)
number of days in luxure accomodation (higher class)
if no number of days in any class, or the number is smaller then lower class is assumed to be the one used
another business rule says that for the first night out (away from their home) staff receives an extra night for their transfer (!generous company!)
i have created a class for holding all this information so far - HotelStay.java
since this is a team information for a given business event i receive anything between 0 to 200 hotel stays.
thus my dataset looks like
teamID
hotelStayStartDt
hotelStayEndDt
daysInLowerClass
daysInHigherClass
my objective are:
to be able to calculate the actual number of stays (including any additions for the first day away)
to be able to calculate number of total days - including over laps - these will be paid in a different rate)
number of days in each hotel class - lower and higher
since i am clueless how to go about it, i would really appreciate assistance here.
below is my HotelStay.java
many thanks,
Nic

here is the class that i have created so far
package businessCalculations.awayStays;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import com.sun.org.apache.xalan.internal.xsltc.trax.SmartTransformerFactoryImpl;
import com.sun.xml.internal.bind.v2.runtime.reflect.Accessor.SetterOnlyReflection;
* @author
public class HotelStay {
     * hotel starting date of stay hotel stays can only start in the past or
     * today as this is not processing future bookings only dates over and above
     * 1990 are considered into this system
    private Date hotelStayStartDt;
     * hotel date end of staying only dates over and above 1990 are considered
     * into this system
    private Date hotelStayEndDt;
     * days for which a person stayed in economic or other lower class
     * accommodation
    private int daysInLowerClass = 0;
     * days for which a person stayed in de-luxe or other higher class
     * accommodation
    private int daysInHigherClass = 0;
     * flags when person was sleeping in another hotel at the beginning of this
     * stay (they went from one hotel to another without going home in betwee
     * if they were they do not receive the added day bonus for transfer
     * (one overnight stay <b>EXTRA</b>) thus if at the beginning of the stay,
     * they were not sleeping in another hotel, they receive an extra night for
     * their transfer
    private boolean isSleepingAwayOverlapping = false;
     * default constructor
     * @param hotelStayStartDt
     * @param hotelStayEndDt
     * @param daysInLowerClass
     * @param daysInHigherClass
    public HotelStay(Date hotelStayStartDt, Date hotelStayEndDt,
         int daysInLowerClass, int daysInHigherClass) {
     boolean outcome = this.addNewHotelStay(hotelStayStartDt, hotelStayEndDt, daysInLowerClass, daysInHigherClass);
     * @param hotelStayStartDt
     * @param hotelStayEndDt
     * @param daysInLowerClass
     * @param daysInHigherClass
     * @return
    public boolean addNewHotelStay(Date hotelStayStartDt, Date hotelStayEndDt, int daysInLowerClass, int daysInHigherClass) {
     if (this.isValidStayDt(hotelStayStartDt) && this.isValidStayDt(hotelStayEndDt)){
         int lengthOfStay = this.getLengthOfStay(hotelStayStartDt, hotelStayEndDt);
         if(lengthOfStay >=1) {
         this.setHotelStayStartDt(hotelStayStartDt);
         this.setHotelStayEndDt(hotelStayEndDt);
         if (
              (daysInHigherClass + daysInLowerClass == 0)
          ||  (daysInHigherClass + daysInLowerClass <= lengthOfStay + 1 )
          this.setDaysInLowerClass(daysInLowerClass);
          this.setDaysInHigherClass(daysInHigherClass);
         } else {
          this.setDaysInLowerClass(daysInLowerClass);
          this.setDaysInHigherClass(daysInHigherClass);
         return true;
     }else{
         return false;
     * @return the admissionDt
    private Date getHotelStayStartDt() {
     return this.hotelStayStartDt;
     * @param admissionDt
     *            the admissionDt to set
    private void setHotelStayStartDt(Date admissionDt) {
     this.hotelStayStartDt = admissionDt;
     * @return the dischargeDt
    private Date getHotelStayEndDt() {
     return this.hotelStayEndDt;
     * @param dischargeDt
     *            the dischargeDt to set
    private void setHotelStayEndDt(Date dischargeDt) {
     this.hotelStayEndDt = dischargeDt;
     * @return the isOverlappingOtherCcStays
    private boolean isOverlappingOtherCcStays() {
     return this.isSleepingAwayOverlapping;
     * @param isOverlappingOtherCcStays
     *            the isOverlappingOtherCcStays to set
    private void setOverlappingOtherCcStays(boolean isOverlappingOtherCcStays) {
     this.isSleepingAwayOverlapping = isOverlappingOtherCcStays;
     * @return the daysInLowerClass
    private int getDaysInLowerClass() {
     return this.daysInLowerClass;
     * @param daysInLowerClass
     *            the daysInLowerClass to set
    private void setDaysInLowerClass(int daysInLowerClass) {
     this.daysInLowerClass = daysInLowerClass;
     * @return the daysInHigherClass
    private int getDaysInHigherClass() {
     return this.daysInHigherClass;
     * @param daysInHigherClass
     *            the daysInHigherClass to set
    private void setDaysInHigherClass(int daysInHigherClass) {
     this.daysInHigherClass = daysInHigherClass;
     * @return the isSleepingAwayOverlapping
    private boolean isSleepingAwayOverlapping() {
     return this.isSleepingAwayOverlapping;
     * @param isSleepingAwayOverlapping
     *            the isSleepingAwayOverlapping to set
    private void setSleepingAwayOverlapping(boolean isSleepingAwayOverlapping) {
     this.isSleepingAwayOverlapping = isSleepingAwayOverlapping;
    @Override
    public String toString() {
     String rtn = "";
     SimpleDateFormat sdf = new SimpleDateFormat("dd/MMM/yyyy hh:mm");
     String inDt = sdf.format(getHotelStayStartDt());
     String outDt = sdf.format(getHotelStayEndDt());
     rtn = "ArrivalDt("
         + (inDt.isEmpty() ? "" : inDt) + ")\t,BookOutDt("
          + (outDt.isEmpty() ? "" : outDt) + ")\t,DaysInLowerClass ("
          + String.valueOf(getDaysInLowerClass())
          + ")\t,DaysInHigerClass ("
          + String.valueOf(getDaysInHigherClass()) + ")";
     return rtn;
    public int getLengthOfStay(Date hotelStayStartDt, Date hotelStayEndDt ){
     Calendar startDt = new GregorianCalendar();
     startDt.setTime(hotelStayStartDt);
     Calendar endDt = new GregorianCalendar();
     endDt.setTime(hotelStayEndDt);
     long startMill = startDt.getTimeInMillis();
     long endMill = endDt.getTimeInMillis();
     // Calculate difference in days
     // divided by (24 hours, 60 minutes, 60 seconds, 1000 milliseconds
     // casted as (int)
     return (int) ((endMill - startMill) / (24 * 60 * 60 * 1000));
    private boolean isValidStayDt(Date aDate) {
     SimpleDateFormat sdf = new SimpleDateFormat("dd/MMM/yyyy");
     Calendar minDt = new GregorianCalendar();
     Calendar maxDt = new GregorianCalendar();
     maxDt.add(Calendar.DATE, +1);
     try {
         minDt.setTime(sdf.parse("01/jan/1990"));
     } catch (ParseException e) {
         e.printStackTrace();
     if (aDate.after(minDt.getTime()) && aDate.before(maxDt.getTime())) {
         return true;
     } else {
         return false;
}Edited by: www.jegue.net on Jul 17, 2010 11:31 AM

Similar Messages

  • To calculate the difference between two periods  in SSM

    Hi,
    I have two questions need to be clarified.
    Question 1: I have KPI named Revenue growth and the formula to calculate is  (current year revenue - previous year revenue).I am using data entry& approval.Is that technically possible to calculate the revenue difference between two periods in SSM?
    Question 2: Is that possible to enter the date as input for the variable?for example i want find the difference between two dates and the difference value is output for my variable.
    Regards
    Bala

    Hi:
    Question 1:
    We have same problem, and what we did is to create:
    - 2 NON KPI:
    R1 = Revenue current year
    R2 = Revenue previous year
    - 1 virtual KPI
    R3 = R2-R1
    Question 2:
    I don't know.
    Regards.

  • Difference between two periods

    Hi All,
    We have two periods ZPERFROM and ZPERTO in the transactional Infocube. While writing FOX, want to calculate the difference between these periods.
    I have written the following code in FOX:
    DATA diff TYPE I.
    diff = ZPERTO - ZPERFROM.
    But it is throwing the error as:
    Formula error: Formula element ZPERTO could not be recognized
    Could you pls guide in getting the difference between the two periods.

    Hi Mayank,
    Here comes the scenario where I need to find out the difference between two periods.
    Ex: We have below records in Infocube.
    WBS     ZPERFROM     ZPERTO     0AMOUNT     0CURRENCY   0FISCPER
    W1        009.2007          012.2007    4000             USD
    W2        001.2008          003.2008    6000             EUR
    Need to distribute the amount equally for all the periods which lie between ZPERFROM and ZPERTO for each WBS. After distribution for the above records, we need to get the result as shown below:
    WBS     ZPERFROM     ZPERTO     0AMOUNT     0CURRENCY   0FISCPER
    W1        009.2007          012.2007    1000             USD                 009.2007
    W1        009.2007          012.2007    1000             USD                 010.2007
    W1        009.2007          012.2007    1000             USD                 011.2007
    W1        009.2007          012.2007    1000             USD                 012.2007
    W2        001.2008          003.2008    2000             EUR                 001.2008
    W2        001.2008          003.2008    2000             EUR                 002.2008
    W2        001.2008          003.2008    2000             EUR                 003.2008
    Could you pls guide in achieving the above result.
    Regards,
    Durga
    Message was edited by:
            Durga BW

  • Calculate the Difference Between two dates excluding weekends and Holidays

    Hi,
    We need to calculate the difference between the two dates by excluding the Local public holidays (It is global and varies across countries) and weekends should not be included in calculation for a business day in OBIEE.
    We have two dates: Open date and close date when ever close date is null we are calculating age based on taking the current timestamp and need to exclude the weekends and Holidays when ever the close date is null.
    Ex:
    Col1 col2 Total
    11/9/2010 2:46:38 PM Null 13
    11/2/2010 8:06:26 PM 11/3/2010 5:37:03 PM 1
    (In the Total we shouldn't include the weekends,holidays)
    Please let me know how to calculate the difference between two dates by excluding the weekends and holidays.
    Thanks
    Edited by: user10441472 on Nov 22, 2010 3:14 PM

    You already asked this question and I answered it...
    Re: calculation of Business day in OBIEE

  • Calculate the difference between date on essbase

    Hi Everbody,
    I want to calculate the difference between 2 date in essbase 9.3. Is any one did this practice ?
    I have done this but that is not a better way. so please share your thought with me.
    Regards
    Vikram Singh

    Hi Vikram,
    Take a look at a post I did a while back at:
    Re: Calculations with dates
    Let me know if that answers your question or you have specific questions once you review that.
    Regards,
    John
    http://www.metavero.com

  • Calculate the difference between Creation date and key date

    Hello
    I want to calculate the difference between creation date of the document with key date(todays date). How can i do that in the query designer. Also then I want to restrict my key figure on this difference if it is =10..please can someone suggest what can be done
    thanks

    Hi Grame...
    For the days calculation ..
    I suggest you to use replacement path ..
    I have the reference that you can see the sample. The case of samples is also about day calculation.
    http://www.sd-solutions.com/documents/SDS_BW_Replacement%20Path%20Variables.html
    Hopefully it can help you a lot.
    Regards,
    Niel.

  • Calculate the Difference between 2 dates

    Please let me know how to Calculate the Difference between 2 dates in Time Management. I have a hire date(U1) and adjusted Service Date(B2)
    I need to Find out the number of Years, Days and Months between these 2 dates
    Thanks a lot in Advance

    Hi Rahul,
    You can take a look at the standard function 'HR_HK_DIFF_BT_2_DATES' and see if it suits your need.
    Regards.
    Francis

  • Calculate the difference between two dates times in infopath form 2013

    Hi,
    I have an infopath 2013 form that contains three fields:
    2 date time and the 3rd contains the difference between the two in hours
    how I can make the difference between the two so that the display will be like this:
    Date Time1           08/21/2014           22:00 
    Date Time2           08/22/2014           1:00
    Diff Field                                             3:00

    Hi,
    Please refer to the following article which matches your requirement exactly.
    Calculate the difference between two date picker controls in InfoPath using rules and formulas - no code!
    Please mark it answered, if your problem resolved.

  • Calculate the difference between two dates

    I would like to calculate the difference between two dates in PL/SQL and return the result as a number of days expressed as an integer value.

    Denes,
    A fair point, I should really have posted this on the SQL forum (I'm new to the forum as well as PL/SQL) but thanks for responding anyway. It does raise a question as to how to implement this in ApEx though.
    I have created the function and am calling it as shown below from the source window of a form. The source type is 'PL/SQL expression or function' and the expression in the source window of the form is:
    calc_date_difference (:p26_c_payment, :p26_c_rec)
    The two parameters being passed are of type date but I'm not sure how to handle the ruturned number and populate the form in ApEx with this value.
    Is it possible to do it this way or am I taking completely the wrong approach?
    Regards
    Sandy
    This is not ApEx related but SQL related:
    CREATE OR REPLACE FUNCTION calc_date_difference (
    p_date_1   VARCHAR2,
    p_date_2   VARCHAR2
    RETURN NUMBER
    v_difference   NUMBER;
    v_sql_err      VARCHAR2 (4000);
    BEGIN
    v_difference := TRUNC (TO_DATE (p_date_1)) - TRUNC
    (TO_DATE (p_date_2));
    RETURN v_difference;
    CEPTION
    WHEN OTHERS
    THEN
    v_sql_err := SQLERRM || CHR (10) || CHR (10) ||
    SQLCODE;
    ND calc_date_difference;and
    SQL> SELECT calc_date_difference ('23.01.2007',
    '20.01.2007') diff
    2    FROM DUAL;
    DIFF
    3
    Denes Kubicek                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           

  • How to read time stamps from a spreadshee​t and calculate the difference between consecutiv​e time stamps?

    Hi,
    I am new to Labview. This question might be a joke to some of you here, but any help would be greatly appreciated. I have a spreadsheet with time stamps and power outputs from a generator. I am supposed to calculate the difference between consecutive time stamps, which will act as a delay for the next power output update that needs to be sent. For example, lets say that I have to following data:
    Time Stamp                    Power Output
    11:00:00 AM                       3kW
    11:00:02 AM                       2.9kW
    11:00:04 AM                       3.2kW
    11:00:06 AM                       3.1kW
    The above data doesn't make any sense, but it is just for the purpose of this question.
    So, I have to read 11:00:00 AM and 3kW initially - 3kW is the initial request that

    Hello
    you can simple subtract one time from the other one and so you get the difference. -> Example
    Mike
    Attachments:
    Unbenannt 2.vi ‏8 KB

  • How to read time stamps from a spreadsheet and calculate the difference between consecutive time stamps?

    Hi,
    I am new to Labview. This question might be a joke to some of you here, but any help would be greatly appreciated. I have a spreadsheet with time stamps and power outputs from a generator. I am supposed to calculate the difference between consecutive time stamps, which will act as a delay for the next power output update that needs to be sent. For example, lets say that I have to following data:
    Time Stamp                    Power Output
    11:00:00 AM                       3kW
    11:00:02 AM                       2.9kW
    11:00:04 AM                       3.2kW
    11:00:06 AM                       3.1kW
    The above data doesn't make any sense, but it is just for the purpose of this question.
    So, I have to read 11:00:00 AM and 3kW initially - 3kW is the initial request that is sent. Then I have to

    Repeated forum post
    Please view http://forums.ni.com/ni/board/message?board.id=170&message.id=294435
    Regards,
    Juan Galindo
    Applications Engineer
    National Instruments

  • To calculate the difference between timings ( PM - AM )

    Hi All,
       Can anyone let me know how to calculate the difference between the time.
      EG : From time : 23:00:00 pm
              To    time : 01:00:00 am
    instead of 2 hours we are getting  22 Hrs
    Thanks in Advance
    Sathyapriya

    Hi Priya,
    How about using FM: HR_PDC_ADD_24_HOURS
    PARAMETERS: p_tim1 TYPE uzeit,                              " 23:00:00
                p_tim2 TYPE uzeit.                              " 01:00:00
    DATA: l_diff TYPE uzeit.
    START-OF-SELECTION.
      IF p_tim1 > p_tim2.
        CALL FUNCTION 'HR_PDC_ADD_24_HOURS'
          CHANGING
            logical_time = p_tim2.
      ENDIF.
      l_diff = p_tim2 - p_tim1.
      WRITE:/ l_diff.
    As you are saying you have 30 fields to calculate, make a subroutime instead of calculating everytimg like:
      PERFORM cal_tim_diff USING p_tim1 p_tim2
                        CHANGING l_diff.
    *&      Form  CAL_TIM_DIFF
    FORM cal_tim_diff  USING    p_tim1 TYPE uzeit
                                p_tim2 TYPE uzeit
                       CHANGING p_diff TYPE uzeit.
      IF p_tim1 > p_tim2.
        CALL FUNCTION 'HR_PDC_ADD_24_HOURS'
          CHANGING
            logical_time = p_tim2.
      ENDIF.
      p_diff = p_tim2 - p_tim1.
    ENDFORM.                    " CAL_TIM_DIFF
    If it still doesnt help, check the data type of the time fields that you are considering.

  • Date difference between Requsition apporval and Po approval in Oracle apps

    My requirement is that l need to get the number of date difference between requisition apporval date(requisition) and po approval date (purchase) excluding holidays how to do it ?
    Thanks in advance..

    One PO could be generated from multiple requisitions. In that case, which requisition would you like to consider?
    One requisition could be converted into multiple POs. In that case, which PO would you like to consider?
    Assuming, there is a one-to-one relationship between your POs and requisitions:
    Link a PO to the corresponding requisition by joining po_distributions_all.req_distribution_id with po_req_distributions_all.requisition_id
    Once you have found the 2 records, get the approved_date from po_headers_all and po_requisition_headers_all
    Then go to bom_calendar_dates for those dates and find the difference between seq_num. That is what you are looking for.
    Sandeep Gandhi

  • How we calculate the date difference between two list in SharePoint 2010

    Hi friend's....I have two list first is list1. In this list i have two coulmn start_date and End_date. In second list2 i have two column HolidayName and Holiday_date. Now i want two find the number of day in list1 Excluding Weekend and Holiday_date(that
    column from list2). How i will do ..? Please help me dosto..

    Thanks for reply...
    I have done the date difference in list1. But i want to Exclude the Holiday_date form list2.
    I have list1 - Start_date , End_date, Number_of_day(Exclude weekend and Holiday_date between Start_date and End_date )
    list2 - HolidayName, Holiday_date
    Now how i will calculate the Number_of_day in first list.

  • How do I calculate the difference between periods in Crosstab

    Using Discoverer 4.1 with 11i General Ledger.
    I want to do a crosstab report that compares the differences in amounts from one period to another.
    The table is:
    ACCOUNT - PERIOD - AMOUNT
    Down the left it will show the account number, across the top it will show this month and last month, and the data point is period net. I want a third column that will show the difference between them.
    Example:
    _______JAN-04___FEB-04__Change
    4000____10.00____15.00____5.00
    5000____20.00____30.00___10.00
    6000____30.00____45.00___15.00
    7000____40.00____60.00___20.00
    Is this possible in Discoverer?

    The only way I know of to do what you are asking is to create a calculated field (previous_period) using the lag function
    lag(period_net,1) over (partition by account order by period) then create another calculated field (change) (period_net - previous_period). You might want to review the syntax of the lag function, I'm not sure it's exactly right, but it's in the ball park.
    Hope this helps. If anyone else is got a better way, I would sure like to know, because this is causing us headaches also.
    Thanks,
    Carl Petersen
    Good Humor - Breyers Ice Cream

Maybe you are looking for

  • Storing and displaying web sites

    Hi, I'm writing an RSS Aggregator and I want to be able to download an HTML web site and present it to the user in a nice way. I know there are some very nice existing browsers out there, but the problem is they all take a URL. Therefore, they requir

  • Automator - Filter Finder Items bug?

    I've only started using Automator a few months ago, but now I ran into a problem, which I think is not my fault, but is a bug in Automator. Here's the scenario: What I have is a Folder Action on the Downloads folder, and the following list of actions

  • Take screenshot in native mac/windows systems

    Hi, I know it is possible to take a screenshot of an AIR window, but is it possible to take a screenshot of a windows/mac desktop? If it is not possible to do that, is this a feature to add in the current roadmap? Thanks, Pau

  • Urgent help please: Error on opening MW project

    Hi I have a similar error message to the one in this thread error loading project One day I was using MW and all was fine, the next day it blows up when trying to open a project. We use CVS - is it possible that the NullPointerException would occur i

  • Serializable interface problems

    I am playing around with the Serializable interface and created a class: import java.io.Serializable; public class SerialMessage implements Serializable {      public byte[] from = new byte[4];      public byte[] to = new byte[4];      public byte[]