Example usage for org.joda.time LocalDateTime LocalDateTime

List of usage examples for org.joda.time LocalDateTime LocalDateTime

Introduction

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

Prototype

public LocalDateTime(Object instant) 

Source Link

Document

Constructs an instance from an Object that represents a datetime.

Usage

From source file:com.axelor.apps.crm.service.EventService.java

License:Open Source License

@Transactional
public void addRecurrentEventsByMonths(Event event, int periodicity, int endType, int repetitionsNumber,
        LocalDate endDate, int monthRepeatType) {
    Event lastEvent = event;//from www  . j ava  2 s .  c om
    if (monthRepeatType == 1) {
        int dayOfMonth = event.getStartDateTime().getDayOfMonth();
        if (endType == 1) {
            int repeated = 0;
            while (repeated != repetitionsNumber) {
                Event copy = eventRepo.copy(lastEvent, false);
                copy.setParentEvent(lastEvent);
                if (copy.getStartDateTime().plusMonths(periodicity).dayOfMonth()
                        .getMaximumValue() >= dayOfMonth) {
                    copy.setStartDateTime(copy.getStartDateTime().plusMonths(periodicity));
                    copy.setEndDateTime(copy.getEndDateTime().plusMonths(periodicity));
                    eventRepo.save(copy);
                    repeated++;
                    lastEvent = copy;
                }
            }
        } else {
            while (!lastEvent.getStartDateTime().plusMonths(periodicity).isAfter(endDate)) {
                Event copy = eventRepo.copy(lastEvent, false);
                copy.setParentEvent(lastEvent);
                if (copy.getStartDateTime().plusMonths(periodicity).dayOfMonth()
                        .getMaximumValue() >= dayOfMonth) {
                    copy.setStartDateTime(copy.getStartDateTime().plusMonths(periodicity));
                    copy.setEndDateTime(copy.getEndDateTime().plusMonths(periodicity));
                    eventRepo.save(copy);
                    lastEvent = copy;
                }
            }
        }
    }

    else {
        int dayOfWeek = event.getStartDateTime().getDayOfWeek();
        int positionInMonth = 0;
        if (event.getStartDateTime().getDayOfMonth() % 7 == 0) {
            positionInMonth = event.getStartDateTime().getDayOfMonth() / 7;
        } else {
            positionInMonth = (event.getStartDateTime().getDayOfMonth() / 7) + 1;
        }

        if (endType == 1) {
            int repeated = 0;
            while (repeated != repetitionsNumber) {
                Event copy = eventRepo.copy(lastEvent, false);
                copy.setParentEvent(lastEvent);
                LocalDateTime nextDateTime = new LocalDateTime(copy.getStartDateTime());
                nextDateTime.plusMonths(periodicity);
                int nextDayOfWeek = nextDateTime.getDayOfWeek();
                if (nextDayOfWeek > dayOfWeek) {
                    nextDateTime.minusDays(nextDayOfWeek - dayOfWeek);
                } else {
                    nextDateTime.plusDays(dayOfWeek - nextDayOfWeek);
                }
                int nextPositionInMonth = 0;
                if (event.getStartDateTime().getDayOfMonth() % 7 == 0) {
                    nextPositionInMonth = event.getStartDateTime().getDayOfMonth() / 7;
                } else {
                    nextPositionInMonth = (event.getStartDateTime().getDayOfMonth() / 7) + 1;
                }
                if (nextPositionInMonth > positionInMonth) {
                    nextDateTime.minusWeeks(nextPositionInMonth - positionInMonth);
                } else {
                    nextDateTime.plusWeeks(positionInMonth - nextPositionInMonth);
                }
                Duration dur = new Duration(copy.getStartDateTime().toDateTime(),
                        copy.getEndDateTime().toDateTime());
                copy.setStartDateTime(nextDateTime);
                copy.setEndDateTime(nextDateTime.plus(dur));
                eventRepo.save(copy);
                repeated++;
                lastEvent = copy;
            }
        } else {
            LocalDateTime nextDateTime = new LocalDateTime(lastEvent.getStartDateTime());
            nextDateTime.plusMonths(periodicity);
            int nextDayOfWeek = nextDateTime.getDayOfWeek();
            if (nextDayOfWeek > dayOfWeek) {
                nextDateTime.minusDays(nextDayOfWeek - dayOfWeek);
            } else {
                nextDateTime.plusDays(dayOfWeek - nextDayOfWeek);
            }
            int nextPositionInMonth = 0;
            if (event.getStartDateTime().getDayOfMonth() % 7 == 0) {
                nextPositionInMonth = event.getStartDateTime().getDayOfMonth() / 7;
            } else {
                nextPositionInMonth = (event.getStartDateTime().getDayOfMonth() / 7) + 1;
            }
            if (nextPositionInMonth > positionInMonth) {
                nextDateTime.minusWeeks(nextPositionInMonth - positionInMonth);
            } else {
                nextDateTime.plusWeeks(positionInMonth - nextPositionInMonth);
            }
            while (!nextDateTime.isAfter(endDate)) {
                Event copy = eventRepo.copy(lastEvent, false);
                copy.setParentEvent(lastEvent);

                Duration dur = new Duration(copy.getStartDateTime().toDateTime(),
                        copy.getEndDateTime().toDateTime());
                copy.setStartDateTime(nextDateTime);
                copy.setEndDateTime(nextDateTime.plus(dur));
                eventRepo.save(copy);
                lastEvent = copy;

                nextDateTime = new LocalDateTime(lastEvent.getStartDateTime());
                nextDateTime.plusMonths(periodicity);
                nextDayOfWeek = nextDateTime.getDayOfWeek();
                if (nextDayOfWeek > dayOfWeek) {
                    nextDateTime.minusDays(nextDayOfWeek - dayOfWeek);
                } else {
                    nextDateTime.plusDays(dayOfWeek - nextDayOfWeek);
                }
                nextPositionInMonth = 0;
                if (event.getStartDateTime().getDayOfMonth() % 7 == 0) {
                    nextPositionInMonth = event.getStartDateTime().getDayOfMonth() / 7;
                } else {
                    nextPositionInMonth = (event.getStartDateTime().getDayOfMonth() / 7) + 1;
                }
                if (nextPositionInMonth > positionInMonth) {
                    nextDateTime.minusWeeks(nextPositionInMonth - positionInMonth);
                } else {
                    nextDateTime.plusWeeks(positionInMonth - nextPositionInMonth);
                }
            }
        }
    }
}

From source file:com.axelor.apps.production.service.ManufOrderWorkflowService.java

License:Open Source License

@Transactional(rollbackOn = { AxelorException.class, Exception.class })
public void pause(ManufOrder manufOrder) {

    if (manufOrder.getOperationOrderList() != null) {

        for (OperationOrder operationOrder : manufOrder.getOperationOrderList()) {

            if (operationOrder.getStatusSelect() == IOperationOrder.STATUS_IN_PROGRESS) {

                operationOrder.setStatusSelect(IOperationOrder.STATUS_STANDBY);

                operationOrder.setStoppedBy(AuthUtils.getUser());

                operationOrder.setStoppingDateTime(new LocalDateTime(generalService.getTodayDateTime()));

            }/*w  ww . j  av a  2s .  c om*/

        }

    }

    manufOrder.setStatusSelect(IManufOrder.STATUS_STANDBY);

    manufOrderRepo.save(manufOrder);

}

From source file:com.axelor.apps.production.service.ManufOrderWorkflowService.java

License:Open Source License

@Transactional(rollbackOn = { AxelorException.class, Exception.class })
public void resume(ManufOrder manufOrder) {

    if (manufOrder.getOperationOrderList() != null) {

        for (OperationOrder operationOrder : manufOrder.getOperationOrderList()) {

            if (operationOrder.getStatusSelect() == IOperationOrder.STATUS_STANDBY) {

                operationOrder.setStatusSelect(IOperationOrder.STATUS_IN_PROGRESS);

                operationOrder.setStartedBy(AuthUtils.getUser());

                operationOrder.setStartingDateTime(new LocalDateTime(generalService.getTodayDateTime()));

            }//w w w. j ava 2 s  . c  o  m

        }

    }

    manufOrder.setStatusSelect(IManufOrder.STATUS_IN_PROGRESS);

    manufOrderRepo.save(manufOrder);

}

From source file:com.axelor.apps.production.service.OperationOrderWorkflowService.java

License:Open Source License

@Transactional(rollbackOn = { AxelorException.class, Exception.class })
public void start(OperationOrder operationOrder) {

    operationOrder.setStatusSelect(IOperationOrder.STATUS_IN_PROGRESS);

    operationOrder.setRealStartDateT(today);

    operationOrder.setStartedBy(AuthUtils.getUser());

    operationOrder.setStartingDateTime(new LocalDateTime(generalService.getTodayDateTime()));

    Beans.get(OperationOrderRepository.class).save(operationOrder);

}

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 a 2s. c om*/
    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 ava  2 s .  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.boha.golfkids.util.NewGolfGroupUtil.java

private static int getPlayerAge(long date) {
    LocalDateTime birthday = new LocalDateTime(date);
    LocalDateTime start = new LocalDateTime();
    Years years = Years.yearsBetween(birthday, start);
    return years.getYears();
}

From source file:com.clicktravel.common.random.Randoms.java

License:Apache License

/**
 * @return A random local date & time, between approximately year 1970 and year 6429.
 *///from w  w w. j  a va  2  s .c o  m
public static LocalDateTime randomLocalDateTime() {
    return new LocalDateTime(randomDateTime());
}

From source file:com.effektif.mongo.MongoHelper.java

License:Apache License

public static LocalDateTime readTime(BasicDBObject dbObject, String fieldName) {
    Object object = dbObject.get(fieldName);
    if (object == null) {
        return null;
    }// ww  w.  j  a va2s .  c om
    if (object instanceof Date) {
        return new LocalDateTime((Date) object);
    }
    throw new RuntimeException("date conversion problem: " + object + " (" + object.getClass().getName() + ")");
}

From source file:com.effektif.mongo.MongoReaderHelper.java

License:Apache License

protected static LocalDateTime getTime(BasicDBObject dbObject, String fieldName) {
    Date date = (Date) dbObject.get(fieldName);
    return (date != null ? new LocalDateTime(date) : null);
}