Example usage for org.joda.time Days daysBetween

List of usage examples for org.joda.time Days daysBetween

Introduction

In this page you can find the example usage for org.joda.time Days daysBetween.

Prototype

public static Days daysBetween(ReadablePartial start, ReadablePartial end) 

Source Link

Document

Creates a Days representing the number of whole days between the two specified partial datetimes.

Usage

From source file:com.ning.billing.invoice.generator.DefaultInvoiceGenerator.java

License:Apache License

/**
 * Add the repair item for the (yet to be) repairedItem. It will merge the candidateRepairItem with reparee item
 *
 *
 *
 * @param repairedItem        the item being repaired
 * @param candidateRepairItem the repair item we would have if we were to repair the full period
 * @param proposedItems       the list of proposed items
 *///from ww  w.j  a v a 2s .  c om
void addRepairItem(final InvoiceItem repairedItem, final RepairAdjInvoiceItem candidateRepairItem,
        final List<InvoiceItem> proposedItems) {

    int nbTotalRepaireeDays = 0;

    // totalRepareeItemAmount is negative and represents the portion left after we removed the adjustments for the total period for all the reparees combined
    BigDecimal totalRepareeItemAmount = candidateRepairItem.getAmount();
    final List<InvoiceItem> reparees = new ArrayList<InvoiceItem>();
    for (final InvoiceItem cur : proposedItems) {
        if (isRepareeItemForRepairedItem(repairedItem, cur)) {
            nbTotalRepaireeDays += Days.daysBetween(cur.getStartDate(), cur.getEndDate()).getDays();
            reparees.add(cur);
            totalRepareeItemAmount = totalRepareeItemAmount.add(cur.getAmount());
        }
    }
    int nbTotalRepairedDays = Days
            .daysBetween(candidateRepairItem.getStartDate(), candidateRepairItem.getEndDate()).getDays()
            - nbTotalRepaireeDays;

    // If we repaired the full period there is no repairee item
    if (reparees.size() == 0) {
        proposedItems.add(candidateRepairItem);
        return;
    }

    // Sort the reparees based on startDate in order to create the repair items -- based on the endDate (previous repairee) -> startDate (next reparee)
    Collections.sort(reparees, new Comparator<InvoiceItem>() {
        @Override
        public int compare(final InvoiceItem o1, final InvoiceItem o2) {
            return o1.getStartDate().compareTo(o2.getStartDate());
        }
    });

    //Build the reparees
    BigDecimal totalRepairItemAmount = BigDecimal.ZERO;
    List<InvoiceItem> repairedItems = new ArrayList<InvoiceItem>();
    InvoiceItem prevReparee = null;
    final Iterator<InvoiceItem> it = reparees.iterator();
    while (it.hasNext()) {
        final InvoiceItem nextReparee = it.next();
        if (prevReparee != null) {
            // repairItemAmount is an approximation of the exact amount by simply prorating totalRepareeItemAmount in the repair period; we make sure last item is calculated based
            // on what is left so the sum of all repairs amount is exactly correct
            final BigDecimal repairItemAmount = (nextReparee.getEndDate()
                    .compareTo(candidateRepairItem.getEndDate()) != 0)
                            ? InvoiceDateUtils
                                    .calculateProrationBetweenDates(prevReparee.getEndDate(),
                                            nextReparee.getStartDate(), nbTotalRepairedDays)
                                    .multiply(totalRepareeItemAmount)
                            : totalRepareeItemAmount.subtract(totalRepairItemAmount);
            totalRepairItemAmount = totalRepairItemAmount.add(repairItemAmount);
            final RepairAdjInvoiceItem repairItem = new RepairAdjInvoiceItem(candidateRepairItem.getInvoiceId(),
                    candidateRepairItem.getAccountId(), prevReparee.getEndDate(), nextReparee.getStartDate(),
                    repairItemAmount, candidateRepairItem.getCurrency(), repairedItem.getId());
            repairedItems.add(repairItem);
        }
        prevReparee = nextReparee;
    }

    // In case we end up with a repair up to the service endDate we need to add this extra item-- this is the 'classic' case with one repairee/repair item
    if (prevReparee.getEndDate().compareTo(candidateRepairItem.getEndDate()) != 0) {
        final BigDecimal repairItemAmount = totalRepareeItemAmount.subtract(totalRepairItemAmount);
        final RepairAdjInvoiceItem repairItem = new RepairAdjInvoiceItem(candidateRepairItem.getInvoiceId(),
                candidateRepairItem.getAccountId(), prevReparee.getEndDate(), candidateRepairItem.getEndDate(),
                repairItemAmount, candidateRepairItem.getCurrency(), repairedItem.getId());
        repairedItems.add(repairItem);
    }

    // Finally remove all reparees from the proposed items and add all repaired items in the invoice
    for (InvoiceItem reparee : reparees) {
        proposedItems.remove(reparee);
    }
    proposedItems.addAll(repairedItems);
}

From source file:com.ning.billing.invoice.generator.InvoiceDateUtils.java

License:Apache License

/**
 *
 * Called internally to calculate proration or when we recalculate approximate repair amount
 *
 * @param startDate                 start date of the prorated interval
 * @param endDate                   end date of the prorated interval
 * @param previousBillingCycleDate  start date of the period
 * @param nextBillingCycleDate      end date of the period
 * @return//w w w .java 2 s  . co m
 */
public static BigDecimal calculateProrationBetweenDates(final LocalDate startDate, final LocalDate endDate,
        final LocalDate previousBillingCycleDate, final LocalDate nextBillingCycleDate) {
    final int daysBetween = Days.daysBetween(previousBillingCycleDate, nextBillingCycleDate).getDays();
    return calculateProrationBetweenDates(startDate, endDate, daysBetween);
}

From source file:com.ning.billing.invoice.generator.InvoiceDateUtils.java

License:Apache License

public static BigDecimal calculateProrationBetweenDates(final LocalDate startDate, final LocalDate endDate,
        int daysBetween) {
    if (daysBetween <= 0) {
        return BigDecimal.ZERO;
    }/*from w w w  . j a v a 2  s .  c  om*/

    final BigDecimal daysInPeriod = new BigDecimal(daysBetween);
    final BigDecimal days = new BigDecimal(Days.daysBetween(startDate, endDate).getDays());

    return days.divide(daysInPeriod, 2 * NUMBER_OF_DECIMALS, ROUNDING_METHOD);
}

From source file:com.nitdlibrary.EditBook.java

public void calculateBookPerformance() throws SQLException {
    issueResultSet.beforeFirst();// w w w  . ja v a2 s  .co m
    java.util.Date returnDate = null, dueDate = null;
    LocalDate returnDateJoda = null, dueDateJoda = null;
    int totalIssued = 0, returned = 0, fine = 0, currentIssue = 0;
    int flag = 0; //incremented when today is > due date and return_date  is null. it means that some books are not returned and fine calc is shown wrt today
    while (issueResultSet.next()) {
        totalIssued++;
        if (issueResultSet.getString("return_date") != null)
            returned++;

        DateFormat format = new SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH);

        try {
            dueDate = format.parse(issueResultSet.getString("due_date"));
            /**
             * IF BOOK HAS NOT BEEN RETURNED AND TODAY>DUEDATE .. FINE TO BE PAID IS SHOWN
             */
            if (issueResultSet.getString("return_date") != null
                    && (issueResultSet.getString("return_date").compareTo("") != 0)) {
                returnDate = format.parse(issueResultSet.getString("return_date"));
            } else {
                String tempDate = format.format(new java.util.Date());
                returnDate = format.parse(tempDate);
                if (dueDate.before(returnDate)) // i.e due date before today and book is not returned.
                    flag++;
            }

            returnDateJoda = new LocalDate(returnDate);
            dueDateJoda = new LocalDate(dueDate);
        } catch (ParseException ex) {
            Logger.getLogger(EditViewStudent.class.getName()).log(Level.SEVERE, null, ex);
        }
        if (dueDate.before(returnDate)) {
            Days d = Days.daysBetween(returnDateJoda, dueDateJoda);
            fine += d.getDays();
        }
        if (issueResultSet.getString("return_date") == null
                || (issueResultSet.getString("return_date").compareTo("") == 0)) {
            currentIssue++;
        }

    }
    /**
    * setting values in Labels
    */
    issued.setText("No of times Issued : " + totalIssued);
    returnedLabel.setText("No of times Returned : " + returned);
    if (fine < 0)
        fine = 0;
    fineLabel.setText("Total Fine Obtained: " + fine);
    if (currentIssue != 0)
        currentLabel.setText("Currently issued : Yes");
    else
        currentLabel.setText("Currently issued : No");
    if (flag != 0)
        exceedLabel.setText("*OverDue : " + flag
                + " student has exceeded due date and has not returned. Assuming he/she retuns today, total fine is being shown.");
    else
        exceedLabel.setText("*");
}

From source file:com.nitdlibrary.EditBook.java

/**
* THIS FUNCTION CALCULATES DATA FOR FILTERED RESULTSET AND UPDATES THE LABELS
* @param from/*from   w w w. j  av  a2 s.  c om*/
* @param to 
*/
private void calculateFilteredPerformance(LocalDate from, LocalDate to) throws SQLException {
    issueFilteredResultSet.beforeFirst();
    java.util.Date returnDate = null, dueDate = null;
    LocalDate returnDateJoda = null, dueDateJoda = null;
    int totalIssued = 0, returned = 0, fine = 0, currentIssue = 0;
    int flag = 0; //incremented when today is > due date and return_date  is null. it means that some books are not returned and fine calc is shown wrt today
    while (issueFilteredResultSet.next()) {
        totalIssued++;
        if (issueFilteredResultSet.getString("return_date") != null)
            returned++;

        DateFormat format = new SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH);

        try {
            dueDate = format.parse(issueFilteredResultSet.getString("due_date"));
            /**
             * IF BOOK HAS NOT BEEN RETURNED AND TODAY>DUEDATE .. FINE TO BE PAID IS SHOWN
             */
            if (issueFilteredResultSet.getString("return_date") != null
                    && (issueFilteredResultSet.getString("return_date").compareTo("") != 0)) {
                returnDate = format.parse(issueFilteredResultSet.getString("return_date"));
            } else {
                String tempDate = format.format(new java.util.Date());
                returnDate = format.parse(tempDate);
                if (dueDate.before(returnDate)) // i.e due date before today and book is not returned.
                    flag++;
            }

            returnDateJoda = new LocalDate(returnDate);
            dueDateJoda = new LocalDate(dueDate);
        } catch (ParseException ex) {
            Logger.getLogger(EditViewStudent.class.getName()).log(Level.SEVERE, null, ex);
        }
        if (dueDate.before(returnDate)) {
            Days d = Days.daysBetween(returnDateJoda, dueDateJoda);
            fine += d.getDays();
        }
        if (issueFilteredResultSet.getString("return_date") == null
                || (issueFilteredResultSet.getString("return_date").compareTo("") == 0)) {
            currentIssue++;
        }
    }
    /**
     * setting values in Labels
     */
    issuedFiltered.setText("No of times Issued : " + totalIssued);
    returnedFiltered.setText("No of times Returned : " + returned);
    if (fine < 0)
        fine = 0;
    fineFiltered.setText("Total Fine Obtained : " + fine);
    if (currentIssue != 0)
        currentFiltered.setText("Remained issued : Yes");
    else
        currentFiltered.setText("Remained issued : No");
    // if(flag!=0){
    //exceedLabel.setText("* "+ flag +" books have exceeded due date and are not returned. Assuming they are retuned today, total fine is being shown.");
    //  }
}

From source file:com.nitdlibrary.EditViewStudent.java

/**
 * THIS FUNCTION CALCULATES STUDENT PERFORMCE AND UPDATES FIELDS
 *//* w  w w . j  a  va 2 s .  co  m*/
private void calculateStudentPerformance() throws SQLException {
    issueResultSet.beforeFirst();
    Date returnDate = null, dueDate = null;
    LocalDate returnDateJoda = null, dueDateJoda = null;
    int totalIssued = 0, returned = 0, fine = 0, currentIssue = 0;
    int flag = 0; //incremented when today is > due date and return_date  is null. it means that some books are not returned and fine calc is shown wrt today
    while (issueResultSet.next()) {
        totalIssued++;
        if (issueResultSet.getString("return_date") != null)
            returned++;

        DateFormat format = new SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH);

        try {
            dueDate = format.parse(issueResultSet.getString("due_date"));
            /**
             * IF BOOK HAS NOT BEEN RETURNED AND TODAY>DUEDATE .. FINE TO BE PAID IS SHOWN
             */
            if (issueResultSet.getString("return_date") != null
                    && (issueResultSet.getString("return_date").compareTo("") != 0)) {
                returnDate = format.parse(issueResultSet.getString("return_date"));
            } else {
                String tempDate = format.format(new Date());
                returnDate = format.parse(tempDate);
                if (dueDate.before(returnDate)) // i.e due date before today and book is not returned.
                    flag++;
            }

            returnDateJoda = new LocalDate(returnDate);
            dueDateJoda = new LocalDate(dueDate);
        } catch (ParseException ex) {
            Logger.getLogger(EditViewStudent.class.getName()).log(Level.SEVERE, null, ex);
        }
        System.out.println(
                "DUE DATE : " + dueDateJoda.toString() + " RETURN DATE : " + returnDateJoda.toString());
        if (dueDate.before(returnDate)) {
            Days d = Days.daysBetween(dueDateJoda, returnDateJoda);
            fine += d.getDays();
            System.out.println("Calculting fine");
        }
        if (issueResultSet.getString("return_date") == null
                || (issueResultSet.getString("return_date").compareTo("") == 0)) {
            currentIssue++;
        }

    }
    /**
     * setting values in Labels
     */
    issued.setText("Total Books Issued : " + totalIssued);
    returnedLabel.setText("Total Books Returned : " + returned);
    if (fine < 0)
        fine = 0;
    fineLabel.setText("Total Fine : " + fine);

    currentLabel.setText("Currently issued book count : " + currentIssue);
    if (flag != 0)
        exceedLabel.setText("* " + flag
                + " books have exceeded due date and are not returned. Assuming they are retuned today, total fine is being shown.");
}

From source file:com.nitdlibrary.EditViewStudent.java

/**
 * THIS FUNCTION CALCULATES DATA FOR FILTERED RESULTSET AND UPDATES THE LABELS
 * @param from/*from   ww  w . j  a v a2 s .c  om*/
 * @param to 
 */
private void calculateFilteredPerformance(LocalDate from, LocalDate to) throws SQLException {
    issueFilteredResultSet.beforeFirst();
    Date returnDate = null, dueDate = null;
    LocalDate returnDateJoda = null, dueDateJoda = null;
    int totalIssued = 0, returned = 0, fine = 0, currentIssue = 0;
    int flag = 0; //incremented when today is > due date and return_date  is null. it means that some books are not returned and fine calc is shown wrt today
    while (issueFilteredResultSet.next()) {
        totalIssued++;
        if (issueFilteredResultSet.getString("return_date") != null)
            returned++;

        DateFormat format = new SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH);

        try {
            dueDate = format.parse(issueFilteredResultSet.getString("due_date"));
            /**
             * IF BOOK HAS NOT BEEN RETURNED AND TODAY>DUEDATE .. FINE TO BE PAID IS SHOWN
             */
            if (issueFilteredResultSet.getString("return_date") != null
                    && (issueFilteredResultSet.getString("return_date").compareTo("") != 0)) {
                returnDate = format.parse(issueFilteredResultSet.getString("return_date"));
            } else {
                String tempDate = format.format(new Date());
                returnDate = format.parse(tempDate);
                if (dueDate.before(returnDate)) // i.e due date before today and book is not returned.
                    flag++;
            }

            returnDateJoda = new LocalDate(returnDate);
            dueDateJoda = new LocalDate(dueDate);
        } catch (ParseException ex) {
            Logger.getLogger(EditViewStudent.class.getName()).log(Level.SEVERE, null, ex);
        }
        if (dueDate.before(returnDate)) {
            Days d = Days.daysBetween(dueDateJoda, returnDateJoda);
            fine += d.getDays();

        }
        if (issueFilteredResultSet.getString("return_date") == null
                || (issueFilteredResultSet.getString("return_date").compareTo("") == 0)) {
            currentIssue++;
        }
    }
    /**
     * setting values in Labels
     */
    issuedFiltered.setText("Total Books Issued : " + totalIssued);
    returnedFiltered.setText("Total Books Returned : " + returned);
    if (fine < 0)
        fine = 0;
    fineFiltered.setText("Total Fine : " + fine);
    currentFiltered.setText("Currently issued book count : " + currentIssue);
    if (flag != 0) {
        //exceedLabel.setText("* "+ flag +" books have exceeded due date and are not returned. Assuming they are retuned today, total fine is being shown.");
    }
}

From source file:com.openkm.util.TimeUtils.java

License:Open Source License

public static int daysBetween(Date first, Date second) {
    return Days.daysBetween(new DateTime(first), new DateTime(second)).getDays();
}

From source file:com.openkm.util.TimeUtils.java

License:Open Source License

public static int daysBetween(Calendar first, Calendar second) {
    return Days.daysBetween(new DateTime(first), new DateTime(second)).getDays();
}

From source file:com.pandits.opensource.JodaTimeUtil.java

License:Open Source License

public int spanIntervalInDays(Date startDate, Date endDate) {
    DateTime startDateTime = createDateTime(startDate);
    DateTime endDateTime = createDateTime(endDate);

    return Days.daysBetween(startDateTime, endDateTime).getDays();
}