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.aurel.track.move.ItemMoveBL.java

License:Open Source License

public static Integer getNumberOfDaysBetweenDates(Date startDateParam, Date endDateParam) {
    DateTime dateTime1 = new DateTime(startDateParam);
    DateTime dateTime2 = new DateTime(endDateParam);
    int numberOfDays = Days.daysBetween(DateTimeUtils.getZeroTimeDateTime(dateTime1),
            DateTimeUtils.getZeroTimeDateTime(dateTime2)).getDays();
    return numberOfDays;
}

From source file:com.aurel.track.util.DateTimeUtils.java

License:Open Source License

/**
 * Returns number of free days from given interval, start date included always, endDate only if includeLastDay == true!
 * @param startDateParam/* w  w w .j av a  2  s . c om*/
 * @param endDateParam
 * @param includeLastDay
 * @return
 */
public static Integer getNumberOfDaysBetweenDates(Date startDateParam, Date endDateParam) {
    DateTime dateTime1 = new DateTime(getZeroTimeDate(startDateParam));
    DateTime dateTime2 = new DateTime(getZeroTimeDate(endDateParam));
    int numberOfDays = Days.daysBetween(dateTime1, dateTime2).getDays();
    return numberOfDays;
}

From source file:com.aurel.track.util.DateTimeUtils.java

License:Open Source License

/**
 * Returns number of free days from given interval, start date included always, endDate only if includeLastDay == true!
 * @param startDateParam/*from   w  w  w .  jav  a2s.  co  m*/
 * @param endDateParam
 * @param includeLastDay
 * @return
 */
public static int getDurationBetweenDates(Date startDateParam, Date endDateParam, boolean onlyWorkdays) {
    DateTime dateTime1 = new DateTime(getZeroTimeDate(startDateParam));
    DateTime dateTime2 = new DateTime(getZeroTimeDate(endDateParam));
    int i = 0;
    if (onlyWorkdays) {
        while (dateTime1.isBefore(dateTime2)) {
            dateTime1 = dateTime1.plusDays(1);
            int dayOfWeek = dateTime1.getDayOfWeek();
            if (dayOfWeek <= 5) {
                //weekday
                i++;
            } else {
                //end date explicitly set on Saturday or Sunday: take this week end day(s) as working day
                if (!dateTime1.isBefore(dateTime2)) {
                    if (dayOfWeek == 6) {
                        //add one day for task ending on Saturday
                        i++;
                    } else {
                        //add two days for task ending on Sunday 
                        i = i + 2;
                    }
                }
            }
        }
        return i;
    } else {
        return Days.daysBetween(dateTime1, dateTime2).getDays() + 1;
    }
}

From source file:com.axelor.apps.organisation.web.TrainingController.java

License:Open Source License

public void computeDuration(ActionRequest request, ActionResponse response) {

    Training training = request.getContext().asType(Training.class);

    if (training.getEndDate() != null && training.getStartDate() != null) {
        Days d = Days.daysBetween(training.getStartDate(), training.getEndDate());
        response.setValue("duration", d.getDays());
    } else {//from  www. j av  a2s  .  com
        response.setValue("duration", null);
    }
}

From source file:com.axelor.apps.production.web.OperationOrderController.java

License:Open Source License

public void chargeByMachineHours(ActionRequest request, ActionResponse response) throws AxelorException {
    List<Map<String, Object>> dataList = new ArrayList<Map<String, Object>>();
    DateTimeFormatter parser = ISODateTimeFormat.dateTime();
    LocalDateTime fromDateTime = LocalDateTime.parse(request.getContext().get("fromDateTime").toString(),
            parser);/* w  w w.ja v  a2 s  .co  m*/
    LocalDateTime toDateTime = LocalDateTime.parse(request.getContext().get("toDateTime").toString(), parser);
    LocalDateTime itDateTime = new LocalDateTime(fromDateTime);

    if (Days.daysBetween(
            new LocalDate(fromDateTime.getYear(), fromDateTime.getMonthOfYear(), fromDateTime.getDayOfMonth()),
            new LocalDate(toDateTime.getYear(), toDateTime.getMonthOfYear(), toDateTime.getDayOfMonth()))
            .getDays() > 20) {
        throw new AxelorException(String.format(I18n.get(IExceptionMessage.CHARGE_MACHINE_DAYS)),
                IException.CONFIGURATION_ERROR);
    }

    List<OperationOrder> operationOrderListTemp = operationOrderRepo.all()
            .filter("self.plannedStartDateT <= ?2 AND self.plannedEndDateT >= ?1", fromDateTime, toDateTime)
            .fetch();
    Set<String> machineNameList = new HashSet<String>();
    for (OperationOrder operationOrder : operationOrderListTemp) {
        if (operationOrder.getWorkCenter() != null && operationOrder.getWorkCenter().getMachine() != null) {
            if (!machineNameList.contains(operationOrder.getWorkCenter().getMachine().getName())) {
                machineNameList.add(operationOrder.getWorkCenter().getMachine().getName());
            }
        }
    }
    while (!itDateTime.isAfter(toDateTime)) {
        List<OperationOrder> operationOrderList = operationOrderRepo.all()
                .filter("self.plannedStartDateT <= ?2 AND self.plannedEndDateT >= ?1", itDateTime,
                        itDateTime.plusHours(1))
                .fetch();
        Map<String, BigDecimal> map = new HashMap<String, BigDecimal>();
        for (OperationOrder operationOrder : operationOrderList) {
            if (operationOrder.getWorkCenter() != null && operationOrder.getWorkCenter().getMachine() != null) {
                String machine = operationOrder.getWorkCenter().getMachine().getName();
                int numberOfMinutes = 0;
                if (operationOrder.getPlannedStartDateT().isBefore(itDateTime)) {
                    numberOfMinutes = Minutes.minutesBetween(itDateTime, operationOrder.getPlannedEndDateT())
                            .getMinutes();
                } else if (operationOrder.getPlannedEndDateT().isAfter(itDateTime.plusHours(1))) {
                    numberOfMinutes = Minutes
                            .minutesBetween(operationOrder.getPlannedStartDateT(), itDateTime.plusHours(1))
                            .getMinutes();
                } else {
                    numberOfMinutes = Minutes.minutesBetween(operationOrder.getPlannedStartDateT(),
                            operationOrder.getPlannedEndDateT()).getMinutes();
                }
                if (numberOfMinutes > 60) {
                    numberOfMinutes = 60;
                }
                BigDecimal percentage = new BigDecimal(numberOfMinutes).multiply(new BigDecimal(100))
                        .divide(new BigDecimal(60), 2, RoundingMode.HALF_UP);
                if (map.containsKey(machine)) {
                    map.put(machine, map.get(machine).add(percentage));
                } else {
                    map.put(machine, percentage);
                }
            }
        }
        Set<String> keyList = map.keySet();
        for (String key : machineNameList) {
            if (keyList.contains(key)) {
                Map<String, Object> dataMap = new HashMap<String, Object>();
                if (Hours.hoursBetween(fromDateTime, toDateTime).getHours() > 24) {
                    dataMap.put("dateTime", (Object) itDateTime.toString("dd/MM/yyyy HH:mm"));
                } else {
                    dataMap.put("dateTime", (Object) itDateTime.toString("HH:mm"));
                }
                dataMap.put("charge", (Object) map.get(key));
                dataMap.put("machine", (Object) key);
                dataList.add(dataMap);
            } else {
                Map<String, Object> dataMap = new HashMap<String, Object>();
                if (Hours.hoursBetween(fromDateTime, toDateTime).getHours() > 24) {
                    dataMap.put("dateTime", (Object) itDateTime.toString("dd/MM/yyyy HH:mm"));
                } else {
                    dataMap.put("dateTime", (Object) itDateTime.toString("HH:mm"));
                }
                dataMap.put("charge", (Object) BigDecimal.ZERO);
                dataMap.put("machine", (Object) key);
                dataList.add(dataMap);
            }
        }

        itDateTime = itDateTime.plusHours(1);
    }

    response.setData(dataList);
}

From source file:com.axelor.apps.production.web.OperationOrderController.java

License:Open Source License

public void chargeByMachineDays(ActionRequest request, ActionResponse response) throws AxelorException {
    List<Map<String, Object>> dataList = new ArrayList<Map<String, Object>>();
    DateTimeFormatter parser = ISODateTimeFormat.dateTime();
    LocalDateTime fromDateTime = LocalDateTime.parse(request.getContext().get("fromDateTime").toString(),
            parser);// w w w .  j a v  a2s. c o  m
    fromDateTime = fromDateTime.withHourOfDay(0).withMinuteOfHour(0);
    LocalDateTime toDateTime = LocalDateTime.parse(request.getContext().get("toDateTime").toString(), parser);
    toDateTime = toDateTime.withHourOfDay(23).withMinuteOfHour(59);
    LocalDateTime itDateTime = new LocalDateTime(fromDateTime);
    if (Days.daysBetween(
            new LocalDate(fromDateTime.getYear(), fromDateTime.getMonthOfYear(), fromDateTime.getDayOfMonth()),
            new LocalDate(toDateTime.getYear(), toDateTime.getMonthOfYear(), toDateTime.getDayOfMonth()))
            .getDays() > 500) {
        throw new AxelorException(String.format(I18n.get(IExceptionMessage.CHARGE_MACHINE_DAYS)),
                IException.CONFIGURATION_ERROR);
    }

    List<OperationOrder> operationOrderListTemp = operationOrderRepo.all()
            .filter("self.plannedStartDateT <= ?2 AND self.plannedEndDateT >= ?1", fromDateTime, toDateTime)
            .fetch();
    Set<String> machineNameList = new HashSet<String>();
    for (OperationOrder operationOrder : operationOrderListTemp) {
        if (operationOrder.getWorkCenter() != null && operationOrder.getWorkCenter().getMachine() != null) {
            if (!machineNameList.contains(operationOrder.getWorkCenter().getMachine().getName())) {
                machineNameList.add(operationOrder.getWorkCenter().getMachine().getName());
            }
        }
    }
    while (!itDateTime.isAfter(toDateTime)) {
        List<OperationOrder> operationOrderList = operationOrderRepo.all()
                .filter("self.plannedStartDateT <= ?2 AND self.plannedEndDateT >= ?1", itDateTime,
                        itDateTime.plusHours(1))
                .fetch();
        Map<String, BigDecimal> map = new HashMap<String, BigDecimal>();
        for (OperationOrder operationOrder : operationOrderList) {
            if (operationOrder.getWorkCenter() != null && operationOrder.getWorkCenter().getMachine() != null) {
                String machine = operationOrder.getWorkCenter().getMachine().getName();
                int numberOfMinutes = 0;
                if (operationOrder.getPlannedStartDateT().isBefore(itDateTime)) {
                    numberOfMinutes = Minutes.minutesBetween(itDateTime, operationOrder.getPlannedEndDateT())
                            .getMinutes();
                } else if (operationOrder.getPlannedEndDateT().isAfter(itDateTime.plusHours(1))) {
                    numberOfMinutes = Minutes
                            .minutesBetween(operationOrder.getPlannedStartDateT(), itDateTime.plusHours(1))
                            .getMinutes();
                } else {
                    numberOfMinutes = Minutes.minutesBetween(operationOrder.getPlannedStartDateT(),
                            operationOrder.getPlannedEndDateT()).getMinutes();
                }
                if (numberOfMinutes > 60) {
                    numberOfMinutes = 60;
                }
                int numberOfMinutesPerDay = 0;
                if (operationOrder.getWorkCenter().getMachine().getWeeklyPlanning() != null) {
                    DayPlanning dayPlanning = weeklyPlanningService.findDayPlanning(
                            operationOrder.getWorkCenter().getMachine().getWeeklyPlanning(),
                            new LocalDate(itDateTime));
                    numberOfMinutesPerDay = Minutes
                            .minutesBetween(dayPlanning.getMorningFrom(), dayPlanning.getMorningTo())
                            .getMinutes();
                    numberOfMinutesPerDay += Minutes
                            .minutesBetween(dayPlanning.getAfternoonFrom(), dayPlanning.getAfternoonTo())
                            .getMinutes();
                } else {
                    numberOfMinutesPerDay = 60 * 8;
                }
                BigDecimal percentage = new BigDecimal(numberOfMinutes).multiply(new BigDecimal(100))
                        .divide(new BigDecimal(numberOfMinutesPerDay), 2, RoundingMode.HALF_UP);
                if (map.containsKey(machine)) {
                    map.put(machine, map.get(machine).add(percentage));
                } else {
                    map.put(machine, percentage);
                }
            }
        }
        Set<String> keyList = map.keySet();
        for (String key : machineNameList) {
            if (keyList.contains(key)) {
                int found = 0;
                for (Map<String, Object> mapIt : dataList) {
                    if (mapIt.get("dateTime").equals((Object) itDateTime.toString("dd/MM/yyyy"))
                            && mapIt.get("machine").equals((Object) key)) {
                        mapIt.put("charge", new BigDecimal(mapIt.get("charge").toString()).add(map.get(key)));
                        found = 1;
                        break;
                    }

                }
                if (found == 0) {
                    Map<String, Object> dataMap = new HashMap<String, Object>();

                    dataMap.put("dateTime", (Object) itDateTime.toString("dd/MM/yyyy"));
                    dataMap.put("charge", (Object) map.get(key));
                    dataMap.put("machine", (Object) key);
                    dataList.add(dataMap);
                }
            }
        }

        itDateTime = itDateTime.plusHours(1);
    }

    response.setData(dataList);
}

From source file:com.axelor.apps.tool.date.DateTool.java

License:Open Source License

private static int daysBetween(LocalDate date1, LocalDate date2) {

    if (date2.isBefore(date1)) {
        return Days.daysBetween(date1, date2).getDays() - 1;
    } else {//from   ww  w  .  j a  v a 2 s  .co  m
        return Days.daysBetween(date1, date2).getDays() + 1;
    }
}

From source file:com.barchart.feed.ddf.resolver.provider.Status.java

License:BSD License

boolean isPending() {

    if (!wasRunSuccess) {
        return true;
    }//from  w w w  .  j  av  a2 s.co m

    final DateTime previous = new DateTime(lastRunTime);

    final DateTime current = new DateTime(DateTimeZone.UTC);

    final Days days = Days.daysBetween(previous, current);

    final int count = days.getDays();

    if (count > 1) {
        return true;
    }

    return false;

}

From source file:com.bgh.myopeninvoice.jsf.jsfbeans.InvoiceBean.java

License:Apache License

public String onFlowProcessTimesheet(FlowEvent event) {
    //this is to reset position and adjust based on the size of data
    RequestContext.getCurrentInstance().execute("PF('invoice-items-timesheet-form-dialog').initPosition();");

    if ("select-date".equalsIgnoreCase(event.getOldStep()) && selectedInvoiceItemsEntity != null) {
        //need to reset in case we changed something
        selectedInvoiceItemsEntity = invoiceDAO.getInvoiceItemsRepository()
                .findOne(selectedInvoiceItemsEntity.getInvoiceItemId());
        final List<TimeSheetEntity> timeSheetsByInvoiceItemId = (List<TimeSheetEntity>) selectedInvoiceItemsEntity
                .getTimeSheetsByInvoiceItemId();

        int days = Days.daysBetween(dateFromTimesheet, dateToTimesheet).getDays() + 1;

        for (int i = 0; i < days; i++) {
            LocalDate potentialLocalDate = dateFromTimesheet.withFieldAdded(DurationFieldType.days(), i);
            TimeSheetEntity e = new TimeSheetEntity();
            e.setInvoiceItemId(selectedInvoiceItemsEntity.getInvoiceItemId());
            e.setInvoiceItemsByInvoiceItemId(selectedInvoiceItemsEntity);
            e.setItemDate(potentialLocalDate.toDate());

            if (timeSheetsByInvoiceItemId.stream()
                    .noneMatch(p -> p.getItemDate().equals(potentialLocalDate.toDate()))) {
                timeSheetsByInvoiceItemId.add(e);
            }/*  w w w.j  a  v a  2 s  .  co m*/
        }
        //need to sort here because we added some values - they will be displayed in order
        timeSheetsByInvoiceItemId.sort((l1, l2) -> l1.getItemDate().compareTo(l2.getItemDate()));

    }
    return event.getNewStep();
}

From source file:com.billing.ng.entities.CurrentBillingCycle.java

License:Open Source License

/**
 * Calculates the number of complete cycles between the given billing start date and
 * the given "today's" date./*from w  w w  .  j  a v  a 2s.  c  o  m*/
 *
 * @param period billing period
 * @param billingStart billing cycle start
 * @param today end date
 * @return number of complete cycles
 */
public static Integer calculateCycleNumber(BillingPeriod period, DateMidnight billingStart,
        DateMidnight today) {
    Integer interval = period.getInterval();

    switch (period.getType()) {
    case DAY:
        return Days.daysBetween(billingStart, today).getDays() / interval;

    case WEEK:
        return Weeks.weeksBetween(billingStart, today).getWeeks() / interval;

    case MONTH:
        return Months.monthsBetween(billingStart, today).getMonths() / interval;

    case YEAR:
        return Years.yearsBetween(billingStart, today).getYears() / interval;
    }
    return null;
}